1

使用 HttpClient 登录后,我正在尝试浏览网站。

我首先定义一个 HttpClient 的实例和一个 cookie 存储:

public HttpClient httpclient = new DefaultHttpClient();
public CookieStore cookieStore = new BasicCookieStore();
public HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

在一个(android)BackgroundTask 中,我成功登录了该网站。

HttpPost httppost = new HttpPost("http://www.deeproute.com/deeproute/default.asp");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("cookieexists","false"));
nameValuePairs.add(new BasicNameValuePair("name", mUser));
nameValuePairs.add(new BasicNameValuePair("password", mPassword));
nameValuePairs.add(new BasicNameValuePair("subbera", "Login"));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
res = httpclient.execute(httppost, localContext);

在此之后,在另一个 BackgroundTask 中,我尝试连接到同一网站上的不同页面:

HttpGet rosterGet = new HttpGet("http://deeproute.com/deeproute/?sel=rosterlook&myleagueno=6&myteamno=12");
res = httpclient.execute(rosterGet, localContext);

但是,当我连接到此页面时,我不再登录。我看了看,成功登录后 cookie 仍然存在,所以我不知所措。

编辑:为了记录,这个网站在普通浏览器上完全正常运行。


编辑 2:响应 Aaron 在下面的回答,cookie 似乎正在保存,因为如果在第一个请求之后,我会这样做:

List<Cookie> cookies = cookieStore.getCookies();
    int cookieSize = cookies.size();

    for (int i = 0; i < cookieSize; i++) {
        Log.v(TAG, "Cookie " + i + "name: "
                                + cookies.get(i).getName());
        Log.v(TAG, "Cookie " + i + "value: "
                                + cookies.get(i).getValue());
    }

我取回了四个 cookie,其中一个存储了我的用户名。如果我在第二个请求后做同样的事情,我实际上会得到 6 个 cookie,因为显然它给了我一个新的会话 ID。这似乎是问题的根源,但我不知道如何解决它。

在第一个请求之后,这是我的会话 ID cookie:

name: ASPSESSIONIDSCSCSBCS
value: GBAJALJBOGKBFLAELPNKEDOE

在第二个请求之后,我有两个会话 ID cookie:

name: ASPSESSIONIDSCSCSBCS
value: GBAJALJBOGKBFLAELPNKEDOE

name: ASPSESSIONIDSCSCSBCS
value: MBAJALJBDBOKPEHNCDDFOCBC
4

2 回答 2

0

我遇到了同样的问题。我实现了 cookie,但它仍然没有保持登录状态。我终于让它工作了,所以我发布了这段代码,希望能帮助其他人解决同样的问题。

以下是 Apache 关于如何登录和检索所有 cookie 的示例:https ://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientFormLogin.java

您还可以在此处查看有关“HTTP 状态管理”的 Apache HttpClient 文档:https ://hc.apache.org/httpcomponents-client-ga/tutorial/html/statemgmt.html

这是我的登录功能的实现方式:

  1. 使用设置代理CredentialsProvider
  2. 使用设置cookieBasicCookieStore
  3. 使用构建自定义 HttpClientCloseableHttpClient
  4. 设置POST 请求:目标、主机、参数、路径
  5. 执行POST 请求/login.html以登录。
  6. 执行 GET 请求以/user查看我们是否正确登录
  7. 打印响应的状态码

    //////// SETUP
    //Setup Proxy (If needed)
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope("proxy.mycompany", 8080),
                new UsernamePasswordCredentials("my_username", "my_password"));
    //Setup Cookies
        BasicCookieStore cookieStore = new BasicCookieStore();
    
    //Build HttpClient
        CloseableHttpClient httpclient = HttpClients.custom()
                .setDefaultCredentialsProvider(credsProvider)
                .setDefaultCookieStore(cookieStore)
                .build();
        HttpHost target = new HttpHost("www.mycompany.com", 80, "http");
        HttpHost proxy = new HttpHost("proxy.mycompany", 8080);
        RequestConfig config = RequestConfig.custom()
                .setProxy(proxy)
                .build();
    try {
    //////// LOGIN REQUEST, POST TO /login.html
    //Start POST Request
        HttpPost httppost = new HttpPost('/login.html');
    //Add parameters to POST Request
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();   
        nameValuePairs.add(new BasicNameValuePair('username','my_username'));  
        nameValuePairs.add(new BasicNameValuePair('password','my_password'));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    //Set Config for POST Request
        httppost.setConfig(config);
    //Execute POST Request
        httpresponse = httpclient.execute(target, httppost);
    //Print Response Code
        try {
           System.out.println("Status Code: "+httpresponse.getStatusLine().getStatusCode());
        } finally {
    //Close HTTP Reponse (must be in a 'finally' block)
            httpresponse.close();
        }
    
    //////// CHECK LOGIN STATUS, GET TO /user
    //Start GET Request
        HttpGet httpget = new HttpGet('/user');
    //Set Config for GET Request
        httpget.setConfig(config);
    //Execute GET Request
        httpresponse = httpclient.execute(target, httpget);
    //Print Response Code
        try {
           System.out.println("Status Code: "+httpresponse.getStatusLine().getStatusCode());
        } finally {
    //Close HTTP Reponse (must be in a 'finally' block)
            httpresponse.close();
        }
    } finally {
        httpclient.close();
    }
    
于 2016-09-08T18:18:37.947 回答
-1

看起来不像您从初始请求中保存的 cookie,因此下一个请求中没有任何内容可以告诉您已登录的站点。您需要一个 cookie 容器。去谷歌上查询。

于 2013-04-27T16:45:34.343 回答