1

我正在尝试从给定的 URL 获取页面,该页面需要经过身份验证才能工作。

我已成功在登录 url 上使用 HttpPost 获取 cookie。我得到这样的cookie,所以看起来还可以:

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

Cookie: [version: 0][name: lan][value: 2057][domain:xxx][path: /][expiry: Thu Dec 13 02:08:52 CET 2012]

现在,当我尝试从已知 url 获取页面时,我最终只得到“登录”页面。

我正在从运行良好的 PHP 转换代码。我每次都放了我认为等效的东西。

    HttpClient httpClient = new DefaultHttpClient();
    httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, CHARSET);

    //      curl_setopt($ch2, CURLOPT_COOKIESESSION, false);
    httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES);

    //      curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, true);
    httpClient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS,true);
    //      curl_setopt($ch2, CURLOPT_MAXREDIRS, 10);
    httpClient.getParams().setParameter(ClientPNames.MAX_REDIRECTS, 10);
    //      curl_setopt($ch2, CURLOPT_USERAGENT, USER_AGENT);
    httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT,USER_AGENT); 
    //      curl_setopt($ch2, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);


    //      curl_setopt($ch2, CURLOPT_COOKIEFILE, COOKIE_FILE);
    Connexion con = new Connexion();
    CookieStore oldCookieStore = con.getCookieStore();
    CookieStore cookieStore = new BasicCookieStore();
    for (Cookie c : oldCookieStore.getCookies()) {
        cookieStore.addCookie(c);
    }

    HttpContext context = new BasicHttpContext();
    context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    HttpGet httpGet = new HttpGet(URL);

    HttpResponse response = httpClient.execute(httpGet, context);

    // Examine the response status
    System.out.println(response.getStatusLine());

    // Get hold of the response entity
    HttpEntity entity = response.getEntity();

    ... ( treat entity )

我还没有找到这些行的等价物,我认为这是与生俱来的:

//        curl_setopt($ch2, CURLOPT_BINARYTRANSFER, true);
//        curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
//        curl_setopt($ch2, CURLOPT_AUTOREFERER, true)

以下是日志,我们可以看到 3 次尝试最终以登录 URL 结束:

DEBUG [org.apache.http.impl.conn.BasicClientConnectionManager] Get connection for route {}->http://xxx
DEBUG [org.apache.http.impl.conn.DefaultClientConnectionOperator] Connecting to xxx:80
DEBUG [org.apache.http.client.protocol.RequestAddCookies] CookieSpec selected: ignoreCookies
DEBUG [org.apache.http.client.protocol.RequestAddCookies] Cookie [version: 0][name: ASP.NET_SessionId][value: wqvdz0bvroatgi45ywpsto2q][domain: xxx][path: /][expiry: null] match [xxx:80/GENERAL.aspx]
DEBUG [org.apache.http.client.protocol.RequestAddCookies] Cookie [version: 0][name: lan][value: 2057][domain: xxx][path: /][expiry: Thu Dec 13 02:08:52 CET 2012] match [xxx:80/GENERAL.aspx]
DEBUG [org.apache.http.client.protocol.RequestAuthCache] Auth cache not set in the context
DEBUG [org.apache.http.client.protocol.RequestTargetAuthentication] Target auth state: UNCHALLENGED
DEBUG [org.apache.http.client.protocol.RequestProxyAuthentication] Proxy auth state: UNCHALLENGED
DEBUG [org.apache.http.impl.client.DefaultHttpClient] Attempt 1 to execute request
DEBUG [org.apache.http.impl.conn.DefaultClientConnection] Sending request: GET /GENERAL.aspx?id=6414 HTTP/1.1
DEBUG [org.apache.http.headers] >> GET /GENERAL.aspx?id=6414 HTTP/1.1
DEBUG [org.apache.http.headers] >> Host: xxx
DEBUG [org.apache.http.headers] >> Connection: Keep-Alive
DEBUG [org.apache.http.headers] >> User-Agent: User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0
DEBUG [org.apache.http.impl.conn.DefaultClientConnection] Receiving response: HTTP/1.1 302 Found
DEBUG [org.apache.http.headers] << HTTP/1.1 302 Found
DEBUG [org.apache.http.headers] << Cache-Control: private
DEBUG [org.apache.http.headers] << Content-Length: 229
DEBUG [org.apache.http.headers] << Content-Type: text/html; charset=utf-8
DEBUG [org.apache.http.headers] << Location: /Default.aspx?ReturnUrl=%2fGENERAL.aspx%3id=6414
DEBUG [org.apache.http.headers] << Server: Microsoft-IIS/7.5
DEBUG [org.apache.http.headers] << X-AspNet-Version: 2.0.50727
DEBUG [org.apache.http.headers] << X-Powered-By: ASP.NET
DEBUG [org.apache.http.headers] << Date: Tue, 13 Nov 2012 01:08:51 GMT
DEBUG [org.apache.http.impl.client.DefaultHttpClient] Connection can be kept alive indefinitely
DEBUG [org.apache.http.impl.client.DefaultRedirectStrategy] Redirect requested to location '/Default.aspx?ReturnUrl=%2fGENERAL.aspx%3id=6414'
DEBUG [org.apache.http.impl.client.DefaultHttpClient] Redirecting to 'http://xxx/Default.aspx?ReturnUrl=%2fGENERAL.aspx%3id=6414' via {}->http://xxx
DEBUG [org.apache.http.client.protocol.RequestAddCookies] CookieSpec selected: ignoreCookies
DEBUG [org.apache.http.client.protocol.RequestAddCookies] Cookie [version: 0][name: ASP.NET_SessionId][value: wqvdz0bvroatgi45ywpsto2q][domain: xxx][path: /][expiry: null] match [xxx:80/Default.aspx]
DEBUG [org.apache.http.client.protocol.RequestAddCookies] Cookie [version: 0][name: lan][value: 2057][domain: xxx][path: /][expiry: Thu Dec 13 02:08:52 CET 2012] match [xxx:80/Default.aspx]
DEBUG [org.apache.http.client.protocol.RequestAuthCache] Auth cache not set in the context
DEBUG [org.apache.http.client.protocol.RequestTargetAuthentication] Target auth state: UNCHALLENGED
DEBUG [org.apache.http.client.protocol.RequestProxyAuthentication] Proxy auth state: UNCHALLENGED
DEBUG [org.apache.http.impl.client.DefaultHttpClient] Attempt 2 to execute request
DEBUG [org.apache.http.impl.conn.DefaultClientConnection] Sending request: GET /Default.aspx?ReturnUrl=%2fGENERAL.aspx%3id=6414 HTTP/1.1
DEBUG [org.apache.http.headers] >> GET /Default.aspx?ReturnUrl=%2fGENERAL.aspx%3id=6414 HTTP/1.1
DEBUG [org.apache.http.headers] >> Host: xxx
DEBUG [org.apache.http.headers] >> Connection: Keep-Alive
DEBUG [org.apache.http.headers] >> User-Agent: User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0
DEBUG [org.apache.http.impl.conn.DefaultClientConnection] Receiving response: HTTP/1.1 302 Found
DEBUG [org.apache.http.headers] << HTTP/1.1 302 Found
DEBUG [org.apache.http.headers] << Cache-Control: private
DEBUG [org.apache.http.headers] << Content-Length: 165
DEBUG [org.apache.http.headers] << Content-Type: text/html; charset=utf-8
DEBUG [org.apache.http.headers] << Location: /LOGIN.aspx
DEBUG [org.apache.http.headers] << Server: Microsoft-IIS/7.5
DEBUG [org.apache.http.headers] << X-AspNet-Version: 2.0.50727
DEBUG [org.apache.http.headers] << X-Powered-By: ASP.NET
DEBUG [org.apache.http.headers] << Date: Tue, 13 Nov 2012 01:08:52 GMT
DEBUG [org.apache.http.impl.client.DefaultHttpClient] Connection can be kept alive indefinitely
DEBUG [org.apache.http.impl.client.DefaultRedirectStrategy] Redirect requested to location '/LOGIN.aspx'
DEBUG [org.apache.http.impl.client.DefaultHttpClient] Redirecting to 'http://xxx/LOGIN.aspx' via {}->http://xxx
DEBUG [org.apache.http.client.protocol.RequestAddCookies] CookieSpec selected: ignoreCookies
DEBUG [org.apache.http.client.protocol.RequestAddCookies] Cookie [version: 0][name: ASP.NET_SessionId][value: wqvdz0bvroatgi45ywpsto2q][domain: xxx][path: /][expiry: null] match [xxx:80/LOGIN.aspx]
DEBUG [org.apache.http.client.protocol.RequestAddCookies] Cookie [version: 0][name: lan][value: 2057][domain: xxx][path: /][expiry: Thu Dec 13 02:08:52 CET 2012] match [xxx:80/LOGIN.aspx]
DEBUG [org.apache.http.client.protocol.RequestAuthCache] Auth cache not set in the context
DEBUG [org.apache.http.client.protocol.RequestTargetAuthentication] Target auth state: UNCHALLENGED
DEBUG [org.apache.http.client.protocol.RequestProxyAuthentication] Proxy auth state: UNCHALLENGED
DEBUG [org.apache.http.impl.client.DefaultHttpClient] Attempt 3 to execute request
DEBUG [org.apache.http.impl.conn.DefaultClientConnection] Sending request: GET /LOGIN.aspx HTTP/1.1
DEBUG [org.apache.http.headers] >> GET /LOGIN.aspx HTTP/1.1
DEBUG [org.apache.http.headers] >> Host: xxx
DEBUG [org.apache.http.headers] >> Connection: Keep-Alive
DEBUG [org.apache.http.headers] >> User-Agent: User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0
DEBUG [org.apache.http.impl.conn.DefaultClientConnection] Receiving response: HTTP/1.1 200 OK
DEBUG [org.apache.http.headers] << HTTP/1.1 200 OK
DEBUG [org.apache.http.headers] << Cache-Control: no-cache
DEBUG [org.apache.http.headers] << Pragma: no-cache
DEBUG [org.apache.http.headers] << Content-Length: 66322
DEBUG [org.apache.http.headers] << Content-Type: text/html; charset=utf-8
DEBUG [org.apache.http.headers] << Server: Microsoft-IIS/7.5
DEBUG [org.apache.http.headers] << X-AspNet-Version: 2.0.50727
DEBUG [org.apache.http.headers] << X-Powered-By: ASP.NET
DEBUG [org.apache.http.headers] << Date: Tue, 13 Nov 2012 01:08:52 GMT
DEBUG [org.apache.http.impl.client.DefaultHttpClient] Connection can be kept alive indefinitely
HTTP/1.1 200 OK
DEBUG [org.apache.http.impl.conn.BasicClientConnectionManager] Releasing connection org.apache.http.impl.conn.ManagedClientConnectionImpl@4cf7c31d
DEBUG [org.apache.http.impl.conn.BasicClientConnectionManager] Connection can be kept alive indefinitely
4

1 回答 1

3

你的代码:

   httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES);

导致 HttpClient 不存储任何 cookie。

我假设服务器通过 cookie 跟踪您的登录,因此由于您没有存储任何内容,因此您似乎总是未经身份验证,因此登录页面。

于 2012-11-13T02:29:25.353 回答