Android:使用DefaultHttpClient登录网站并保留会话/ Cookie

前端之家收集整理的这篇文章主要介绍了Android:使用DefaultHttpClient登录网站并保留会话/ Cookie前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经通过不同的教程和这个网站,但找不到一个正确的解决方案.另一方面,我看到应用程序登录到网站并要求进一步的信息,所以我确定有办法让这个工作,但也许我的方法错误的.

这是我要做的:我想登录一个需要用户身份验证的网站,然后阅读并解析仅当用户登录时才能访问的网站.
问题:在将凭据发送到网站后,我收到一个似乎不保存在我的HttpClient中的cookie,尽管文档建议应该会发生.

这是我的一些代码

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(LOGIN_URL);

List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair(USER_FIELD,login));
nvps.add(new BasicNameValuePair(PASS_FIELD,pw));
nvps.add(new BasicNameValuePair(REMEMBERME,"on"));

httpost.setEntity(new UrlEncodedFormEntity(nvps,HTTP.UTF_8));

HttpResponse response = httpclient.execute(httpost);
HttpEntity entity = response.getEntity();

if (entity != null) {
  entity.consumeContent();
}

List<Cookie> cookies = httpclient.getCookieStore().getCookies();

当我输出“cookies”的内容时,一切似乎都很好(我收到一个会话):

– [version:0] [name:ASP.NET_SessionId] [value:xxx] [domain:xxx] [path:/] [expiry:null]

据我所知,只要我没有关闭它,Cookie /会话将被保留并在我的HttpClient中使用.

当阅读下一页(受限制)时,使用以下代码

HttpGet httpget2 = new HttpGet(RESTRICTED_URL);
response = httpclient.execute(httpget2);
entity = response.getEntity();
InputStream data = entity.getContent();
// data will be parsed here
if (entity != null) {
    entity.consumeContent();
}
// connection will be closed afterwards

如果我输出GET请求的响应(使用response.getStatusLine()),我得到一个“200 OK”消息,但解析返回的站点显示登录丢失(我只检索登录表单).

任何帮助是赞赏.

解决方法

在我必须登录的应用程序.首先,我必须运行一个GET,然后是一个POST,然后再次GET.第一个获取将实例化一个Jsession Id用于我的连接. POST将验证我的ID,然后原始的GET将返回真实的内容.

以下代码是用于在JBoss中运行的应用程序

public boolean login() {
    HttpGet  httpGet = new HttpGet(  "http://localhost:8080/gwt-console-server/rs/identity/secure/sid/");
    HttpPost httpPost = new HttpPost("http://localhost:8080/gwt-console-server/rs/identity/secure/j_security_check");
    HttpResponse response = null;

    List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair(USER_FIELD,userName));
    nvps.add(new BasicNameValuePair(PASS_FIELD,password));

    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nvps,HTTP.UTF_8));

        response = httpClient.execute(httpGet);
        EntityUtils.consume(response.getEntity());

        response = httpClient.execute(httpPost);
        EntityUtils.consume(response.getEntity());

        response = httpClient.execute(httpGet);
        String sessionId =EntityUtils.toString(response.getEntity());

        String cookieId =""; 
        List<Cookie> cookies = ((AbstractHttpClient) httpClient).getCookieStore().getCookies();
        for (Cookie cookie: cookies){
            if (cookie.getName().equals("JSESSIONID")){
                cookieId = cookie.getValue();
            }
        }

        if(sessionId!= null && sessionId.equals(cookieId) ){
            return true;
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return false;   
}

猜你在找的Android相关文章