Android:登录网站并使用 DefaultHttpClient 保留 session /cookie

标签 android session cookies authentication httpclient

我浏览了不同的教程和这个网站,但找不到合适的解决方案。另一方面,我看到应用程序登录网站并请求更多信息,所以我确信有办法让它工作,但也许我的方法全错了。

这是我正在尝试做的事情:我想登录一个需要用户身份验证的网站,然后读取和解析只有在用户登录后才能访问的网站。 问题:在将凭据发布到网站后,我收到一个 cookie,它似乎没有保留在我的 HttpClient 中,尽管文档表明应该发生这种情况。

这是我的一些代码:

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”的内容时,一切似乎都很好(我收到一个 session ):

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

据我所知,只要我不关闭 cookie/session,它就会在我的 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。 First get 将为我的连接实例化一个 Jsession Id。 POST会验证我的ID,然后原来的get 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:登录网站并使用 DefaultHttpClient 保留 session /cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3434525/

相关文章:

java - 更改 cookie 的值或删除 jax-rs 端点内的 cookie

java - 无法从 api 获取数据到 fragment 中的 Gridview

android - 如何在android中滚动时将标签从内容粘贴到屏幕顶部

android - 在android中为ListView中的每一行制作动态布局

angularjs - 使用 Laravel 作为 API 时每个用户创建的大量 session

PHP:检测用户语言并能够更改语言

java - flash如何通过J2EE认证——它无法发送包含jsessionid的cookie

java - 在这种情况下,当仅调整图像(png)大小时,BitmapFactory 真的有必要吗?

java - hibernate 中的问题 : Session is closed

windows - 如何查询正在运行的进程的登录类型?