HttpClient发布会话问题?我如何知道会话是否已创建?

时间:2010-08-25 01:21:50

标签: android post httpclient

这是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按钮将我发送到你必须登录才能访问的页面时,我得到了没有登录页面。有什么想法吗?

2 个答案:

答案 0 :(得分:4)

我知道,有点晚了,但我也遇到过这个帖子,我想在这里回答这个问题:Android session management说你应该重用你处理会话管理的HttpClient ,即。使HttpClient静态并仅将其实例化一次。 但我不知道这是否符合您的要求。对我来说是。

干杯!

答案 1 :(得分:0)

经过大量研究和后来的许多例子,我终于设法登录了我正在尝试的网站。显然这是一个问题,我没有从响应中消耗实体,即cookie。我创建了一个简单的活动,其主要目的是对网站进行GET和POST,然后将结果吐出到LogCat中。也许这可能有助于其他人。

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,“Check for login:”+ Parser.request(response));通过解析器类打印出网站的html,我用它来验证它实际上是一个需要成功登录的页面