这是 Sending html commands over httpclient android 的后续问题,我已成功发布到服务器并收到 200 代码,但是当我尝试移动到另一个页面时,它无法识别我已经登录。我想知道这是 session 问题还是我是否需要在邮政。我将如何进行重定向?再次非常感谢任何帮助。这是我根据示例创建的一个简单的 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 按钮时,它会将我发送到一个你必须登录才能访问的页面,我得到了未登录页面。有什么想法吗?
最佳答案
我知道,有点晚了,但我也看到了这个帖子,我想要这个答案的链接:Android session management这表示您应该重用处理 session 管理的 HttpClient
,即。使 HttpClient
静态并只实例化一次。
但我不知道这是否符合您的要求。对我来说是。
干杯!
关于android - HttpClient 后 session 问题?我如何知道 session 是否已创建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3562213/