2011-08-21 182 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(); 
    } 
}