HttpClient 会话后问题?我如何知道会话是否已创建?
这是 通过 httpclient android 发送 html 命令 的后续问题,我有成功发布到服务器并收到 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 按钮时,该按钮将我发送到一个您必须登录才能访问的页面,我得到了未登录在页面中。有什么想法吗?
This is a follow up question of Sending html commands over httpclient android , I have successfully POSTed to the server and recieved 200 code but when I attempt to move to another page it does not recognize that I have logged in. I am wondering if it is a session issue or if i need to follow the redirect after the POST. How would I go about following a redirect? Again any help is greatly appreciated. Here is a simple HttpClient / POST app I created from examples in order to help me quickly test any changes.
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!");
}
}
}
I first press the login button on the UI which gives me the Http/1.1 200 OK response code, but when I press the btnFetch button which sends me to a page in which you must but logged in to access, I get the not logged in page. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道,有点晚了,但我也看到了这篇文章,我想在这里找到这个答案的链接: Android 会话管理 表示您应该重用处理会话管理的
HttpClient
,即。将HttpClient
设为静态并仅实例化一次。但我不知道这是否符合您的要求。对我来说是的。
干杯!
I know, quite a little bit late, but I also come across this posting and I want the link to this answere here: Android session management which says that you should reuse your
HttpClient
which handles Session Management, ie. makeHttpClient
static and instantiate it only once.But I dont know if this is the whole truth for your requirements. For me it was.
Cheers!
经过大量研究和许多示例之后,我终于成功登录到我正在尝试的网站。显然,这是我没有消耗响应中的实体(即cookie)的问题。我创建了一个简单的活动,其主要目的是获取并发布到网站,然后将结果输出到 LogCat 中。也许这可能对其他人有帮助。
最后一条日志,Log.d(TAG, "检查登录:" + Parser.request(response));通过解析器类打印出网站的 html,我用它来验证它实际上是一个需要成功登录的页面
After much research and many examples later I have finally managed to login to the site I was attempting. Apparently It was an issue with me not consuming the entity from the response, ie the cookies. I created a simple activity who's main purpose is to GET and POST to the site then spit out the result into LogCat. Perhaps this may help others.
the last log, Log.d(TAG, "Check for login: " + Parser.request(response)); prints out the html of the site by going through a parser class, which I used to verify that it was infact a page that required successful login