【发布时间】:2010-08-25 01:21:50
【问题描述】:
这是Sending html commands over httpclient android 的后续问题,我已成功发布到服务器并收到 200 代码,但是当我尝试移动到另一个页面时,它无法识别我已登录。我想知道是不是会话问题,或者如果我需要在 POST 之后遵循重定向。我将如何进行重定向?再次非常感谢任何帮助。这是我从示例中创建的一个简单的 HttpClient / POST 应用程序,以帮助我快速测试任何更改。
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!");
}
}
}
我首先按下 UI 上的登录按钮,它给了我 Http/1.1 200 OK 响应代码,但是当我按下 btnFetch 按钮将我发送到您必须登录才能访问的页面时,我得到了未登录页面。有什么想法吗?
【问题讨论】:
-
您需要向编写您正在访问的 Web 应用程序的人提出这个问题。只有他们才能告诉您他们希望您如何做您想做的事情。
-
我害怕这样。感谢您的回复。
-
可能是一些cookie问题..请检查
-
我正在研究找出 cookie,我开始认为这可能是一个“推荐人”问题。我将尝试编辑标题以完全模仿 Firefox。
标签: android post httpclient