2011-08-21 184 views
12

我正在試驗本網站以在歡迎頁面上收集我的用戶名以學習Jsoup和Android。使用以下代碼用於HTTPS抓取的Jsoup Cookie

Connection.Response res = Jsoup.connect("http://www.mikeportnoy.com/forum/login.aspx") 
    .data("ctl00$ContentPlaceHolder1$ctl00$Login1$UserName", "username", "ctl00$ContentPlaceHolder1$ctl00$Login1$Password", "password") 
    .method(Method.POST) 
    .execute(); 
String sessionId = res.cookie(".ASPXAUTH"); 

Document doc2 = Jsoup.connect("http://www.mikeportnoy.com/forum/default.aspx") 
.cookie(".ASPXAUTH", sessionId) 
.get(); 

我的cookie(.ASPXAUTH)總是以NULL結尾。如果我在網頁瀏覽器中刪除這個cookie,我會失去聯繫。所以我相信這是正確的cookie。此外,如果我更改代碼

.cookie(".ASPXAUTH", "jkaldfjjfasldjf") Using the correct values of course 

我可以從此頁面中刪除我的登錄名。這也讓我覺得我有正確的cookie。那麼,我的cookie怎麼會出現空?我的用戶名和密碼名稱字段是否有誤?還有別的嗎?

謝謝。

回答

0

如果你試圖獲取和傳遞所有Cookie而不承擔這樣的事:Sending POST request with username and password and save session cookie

如果您仍然有問題嘗試尋找到這一點:Issues with passing cookies to GET request (after POST)

+1

我試過第一個鏈接,並能夠找回三個餅乾,但其中一個是空的。我需要的cookie不在那裏,這就解釋了爲什麼我總是得到NULL。我無法弄清楚爲什麼我的代碼不會返回我在firebug中看到的所有cookie。有什麼我可以找的? – Brian

28

我知道我有點晚了10個月這裏。但是,使用Jsoup一個很好的選擇是使用易peasy一段代碼:

//This will get you the response. 
Response res = Jsoup 
    .connect("url") 
    .data("loginField", "[email protected]", "passField", "pass1234") 
    .method(Method.POST) 
    .execute(); 

//This will get you cookies 
Map<String, String> cookies = res.cookies(); 

//And this is the easieste way I've found to remain in session 
Documente doc = Jsoup.connect("url").cookies(cookies).get(); 

雖然我仍然有麻煩連接到一些網站,我連接到一大堆他們具有相同的基本代碼段。哦,在我忘記之前..我認爲我的問題是SSL證書。你必須以我還沒有弄清楚的方式妥善管理它們。

9

我總是做這兩個步驟(如正常的人),

  1. 閱讀登錄頁面(通過GET,讀取cookies)
  2. 提交表單和Cookie(通過郵寄,沒有cookie的操縱)

示例:從previuos

Connection.Response response = Jsoup.connect("http://www.mikeportnoy.com/forum/login.aspx") 
     .method(Connection.Method.GET) 
     .execute(); 

response = Jsoup.connect("http://www.mikeportnoy.com/forum/login.aspx") 
     .data("ctl00$ContentPlaceHolder1$ctl00$Login1$UserName", "username") 
     .data("ctl00$ContentPlaceHolder1$ctl00$Login1$Password", "password") 
     .cookies(response.cookies()) 
     .method(Connection.Method.POST) 
     .execute(); 

Document homePage = Jsoup.connect("http://www.mikeportnoy.com/forum/default.aspx") 
     .cookies(response.cookies()) 
     .get(); 

並始終設定Cookie請求下一個使用

  .cookies(response.cookies()) 

SSL在這裏並不重要。如果您對certifcates有問題,請執行此方法以忽略SSL。

public static void trustEveryone() { 
    try { 
     HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { 
      public boolean verify(String hostname, SSLSession session) { 
       return true; 
      } 
     }); 

     SSLContext context = SSLContext.getInstance("TLS"); 
     context.init(null, new X509TrustManager[]{new X509TrustManager() { 
      public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } 

      public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } 

      public X509Certificate[] getAcceptedIssuers() { 
       return new X509Certificate[0]; 
      } 
     }}, new SecureRandom()); 
     HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory()); 
    } catch (Exception e) { // should never happen 
     e.printStackTrace(); 
    } 
}