2013-08-27 38 views
2

我搜索了很長時間,並沒有得到任何對我的問題真正有用的東西。 I used this as a guide,向下滾動到「3. Apache HttpClient - 自動登錄Google」。Java Apache HttpClient一些Cookies丟失

當您使用Chrome瀏覽器時,您可以查找用於當前頁面的Cookie。 登錄並瀏覽網站的其他頁面後,這些Cookie遠遠超過HttpClient在登錄和瀏覽其他頁面後獲得的信息。 HttpClient在Chrome中只顯示十一箇中的兩個。 這些頁面需要這些Cookie,因爲我收到的HTML僅包含您在未登錄時獲得的預覽。
某些缺失的Cookie稱爲「userauth_name」,「member_id」等。我認爲他們是必要的登錄,對吧? :P。

唯一的事情,我從該引導代碼改變是:
- 當然了登錄和其他頁面的URL加載
- 在sendPost:
主機:post.setHeader(「主機「,」accounts.google.com「);
引用者:post.setHeader(「Referer」,「https://accounts.google.com/ServiceLoginAuth」);
- 在getFormParams我的測試頁上
形式的標籤沒有id屬性,所以我做了這一點:在指南中有在代碼所做的基本步驟

Elements forms = doc.getElementsByTag("form"); 
    Element loginform = null; 
    for (Element inputElement : forms) { 
     if (inputElement.attr("name").equals("authform")) 
      loginform = inputElement; 
    } 
    Elements inputElements = loginform.getElementsByTag("input"); 

    List<NameValuePair> paramList = new ArrayList<NameValuePair>(); 

    for (Element inputElement : inputElements) { 
     String key = inputElement.attr("name"); 
     String value = inputElement.attr("value"); 

     if (key.equals("username")) 
      value = username; 
     else if (key.equals("password")) 
      value = password; 

     paramList.add(new BasicNameValuePair(key, value));   

    } 

標題後:
1.發送GET請求以獲取登錄表單。 (我得到responecode = 200)
2.使用jsoup html分析器來抓取表單輸入。
3.構造參數併發出POST請求進行身份驗證。 (responecode = 200)
4.向Gmail發送另一個GET請求。 (responecode = 200)

Ther沒有服務器錯誤或Java錯誤。

再次提出的問題:爲什麼有一些Cookies丟失?

+0

我有點解決了我的問題。我只是將所有需要的Cookies手動添加到Cookiestore中作爲「新的BasicClientCookie」。我仍然希望看到其他解決方案,而不是手動添加它。 – wolfi1571883

回答

1

由於違反了實際的cookie管理政策,某些cookie可能會被拒絕。通常情況下,通過打開上下文日誌記錄很容易發現,如this guide中所述。