安卓:登录到网站和preserve会话/ cookie的使用DefaultHttpClient网站、preserve、DefaultHttpClient、cookie

2023-09-05 02:43:46 作者:吃飯就不買單

我已经通过不同的教程和这个网站,但无法找到一个妥善的解决办法。在另一方面,我见过的应用程序登录到网站,并要求进一步的信息,所以我敢肯定有办法得到这个工作,但也许我的做法是完全错误的。

I've been through different tutorials and this website, but couldn't find a proper solution. On the other hand, I've seen apps logging into websites and requesting further information, so I'm sure there's a way to get this working, but maybe my approach is all wrong.

下面就是我想要做的:我想登录到一个网站,需要用户身份验证,然后读取并解析,如果用户登录该才可访问的网站。 问题:过帐凭证到网站后,我收到这似乎并不为preserved在我的HttpClient的一个cookie,即使文档表明,正是应该发生

Here's what I'm trying to do: I want to log into a website that needs user authentication and then read and parse websites that are only accessible if the user is logged in. The problem: after POSTing the credentials to the website, I receive a cookie which doesn't seem to be preserved in my HttpClient, even though the docs suggest that exactly that should happen.

下面是我的一些code:

Here's some of my code:

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();

当我输出曲奇的内容,一切似乎都很好(我收到一个会话):

When I output the contents of "cookies", everything seems fine (I receive a session):

- [版本:0] [名称:ASP.NET_SessionId] [值:XXX] [域:XXX] [路径:/] [到期日期null]

据我了解,该Cookie /会议将是preserved在用我的HttpClient只要我不关闭它。

As I understood, the cookie/session will be preserved and used in my HttpClient as long as I don't close it.

当读取下一个页面(这是受限制的),使用这个code:

When reading the next page (which is restricted), using this code:

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的消息,但解析返回的网站显示,该登录丢失(我才捡回一个登录表单)。

If I output the response of the GET-request (using response.getStatusLine()) I get a "200 OK" message, but parsing the site that is returned shows, that the login is lost (I only retrieve a login form).

任何帮助是AP preciated。

Any help is appreciated.

推荐答案

在我需要登录到一个应用程序。首先,我必须运行一个GET,接着通过后,然后再次获取。第一个GET将实例化一个Jsession ID为我的连接​​。在POST将验证我的身份证,然后在原获得GET将返回的真实内容。

In an application that I have to login to. First i have to run a GET followed by a POST and then the GET again. The First get will instantiate a Jsession Id for my connection. The POST will authenticate my ID and then the original get GET will return the real content.

在code以下是JBoss中运行的应用程序

The code below is for an app running in 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;   
}