HttpClient的帖子会话的问题?如何知道自己是否会被创建? [英] HttpClient Post session issue? How do I know if session has been created?

查看:126
本文介绍了HttpClient的帖子会话的问题?如何知道自己是否会被创建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个后续问题的<一个href="http://stackoverflow.com/questions/3550593/sending-html-commands-over-httpclient-android">http://stackoverflow.com/questions/3550593/sending-html-commands-over-httpclient-android ,我已经成功地发布到服务器的和接收的200 code,但是当我试图移动到另一页不承认,我已经登录。我想知道,如果它是一个会话问题,或者如果我需要跟随在POST后重定向。我怎么会去下面的重定向?再次任何帮助是极大的AP preciated。下面是一个简单的HttpClient / POST应用程序从我的例子,以帮助我快速测试的任何更改创建的。

This is a follow up question of http://stackoverflow.com/questions/3550593/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!");
    }
}

}

我第一次preSS UI上的登录按钮,让我的HTTP / 1.1 200 OK响应code,但是当我preSS的btnFetch按钮,给我发来一个网页,必须在其中但登录后才能访问,我得到的尚未登录页面。任何想法?

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?

推荐答案

在大量的研究和许多例子后,我终于成功地登录到该网站,我试图。显然,这是一个问题,我没有从响应消费实体,即饼干。我创建了一个简单的活动谁的主要目的是GET和POST到网站,然后吐了出来,结果到LogCat中。或许,这可以帮助别人。

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.

public class HttpClientTest extends Activity{

DefaultHttpClient client = new DefaultHttpClient();
HttpGet request;
HttpEntity entity;
List<Cookie> cookies;
HttpResponse response;
HttpPost post;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        getRequest();
    } catch (Exception e) {
        Log.d("My Activity", "Failed");
        e.printStackTrace();
    }
}

public void getRequest() throws Exception
{
    final String TAG = "MyActivity";
    request = new HttpGet("http://some.site.com/");
    response = client.execute(request);
    entity = response.getEntity();
    Log.d(TAG, "Login form get: " + response.getStatusLine());
    if(entity != null)
    {
        entity.consumeContent();
    }
    Log.d(TAG, "Initial set of cookies:");

    cookies = client.getCookieStore().getCookies();
    if (cookies.isEmpty())
    {
        Log.d(TAG, "None");
    }
    else
    {
        for(int i = 0; i<cookies.size(); i++)
        {
            Log.d(TAG, "- " + cookies.get(i));
        }
    }
    String action = "i.cfm?&1028&p=login&se=4";
    String yourServer = "http://some.site.com/";
    post = new HttpPost(yourServer + action);

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("nic", "username"));
    params.add(new BasicNameValuePair("password", "password"));
    params.add(new BasicNameValuePair("server", "4"));

    post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));


    response = client.execute(post);
    entity = response.getEntity();

    Log.d(TAG, "Login form get: " + response.getStatusLine());
    if(entity != null){
        entity.consumeContent();
    }

    Log.d(TAG, "Post logon cookies:");
    cookies = client.getCookieStore().getCookies();
    if (cookies.isEmpty())
    {
        Log.d(TAG, "None");
    } 
    else
    {
        for (int i = 0; i < cookies.size(); i++) 
        {
            Log.d(TAG, "- " + cookies.get(i));
        }
    }


    request = new HttpGet("http://some.site.com/i.cfm?f=com_empire&cm=3");

    response = client.execute(request);
    Log.d(TAG, "Check for login: " + Parser.request(response));
    if(entity != null)
    {
        entity.consumeContent();
    }

}

}

最后一个日志,Log.d(TAG,检查登入:+ Parser.request(响应));打印出网站的HTML所经历一个解析类,这是我用来验证,这是逸岸一个页面,需要登录成功

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

这篇关于HttpClient的帖子会话的问题?如何知道自己是否会被创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆