這是Sending html commands over httpclient android的後續問題,我已成功發佈到服務器並收到200個代碼,但是當我嘗試移動到另一個頁面時,它無法識別我已登錄。我想知道它是否是會話問題或者如果我需要在POST後關注重定向。我將如何去關於重定向?再次,任何幫助,不勝感激。這裏是一個簡單的HttpClient/POST應用程序,我通過示例創建,以幫助我快速測試任何更改。HttpClient發佈會話問題?我如何知道會話是否已創建?
public class HttpClientTest extends Activity{
HttpClient client = new DefaultHttpClient();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button btnFetch = (Button)findViewById(R.id.button);
final TextView txtResult = (TextView)findViewById(R.id.content);
final Button login = (Button)findViewById(R.id.button2);
btnFetch.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
getRequest(txtResult);
}
});
login.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
try {
login(txtResult);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public void getRequest(TextView txtResult){
HttpGet request = new HttpGet("http://gc.gamestotal.com/i.cfm?f=com_empire&cm=3");
try{
HttpResponse response = client.execute(request);
txtResult.setText(Parser.request(response));
}catch(Exception ex){
txtResult.setText("Failed!");
}
}
public void login(TextView txtResult) throws UnsupportedEncodingException, IOException{
String action = "i.cfm?&1028&p=login&se=4";
String yourServer = "http://gc.gamestotal.com/";
HttpPost post = new HttpPost(yourServer + action);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("nic", "user"));
params.add(new BasicNameValuePair("password", "password"));
params.add(new BasicNameValuePair("server", "4"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
post.setEntity(entity);
try{
HttpResponse response = client.execute(post);
txtResult.setText(response.getEntity().toString());
}catch(Exception ex){
txtResult.setText("Failed!");
}
}
}
我第一次按上給我的HTTP/1.1 200 OK響應代碼UI中的登錄按鈕,但是當我按下btnFetch按鈕,送我到一個頁面中,您必須但登錄訪問,我得到沒有登錄頁面。有任何想法嗎?
你需要問這個問題誰寫了你正在訪問的Web應用程序。只有他們可以告訴你他們希望你做你想做的事情。 – CommonsWare 2010-08-25 04:41:47
我很害怕這樣的。感謝您的答覆。 – 2010-08-25 04:54:23
可能是一些cookie問題..請檢查 – Vinay 2010-08-25 14:00:56