2011-09-25 62 views
2

登錄後我想登錄到網站(雅虎郵件 - https://login.yahoo.com/config/login?.src=fpctx&.intl=us&.done=http%3A%2F%2Fwww.yahoo.com%2F)使用的HttpClient獲取內容的Java

並登錄後,我想檢索的內容。 (JAVA)。我的代碼有什麼問題?

public class TestHttpClient { 

public static void main(String[] args) throws Exception { 

    DefaultHttpClient httpclient = new DefaultHttpClient(); 

    HttpGet httpget = new HttpGet("http://www.yahoo.com/"); 

    HttpResponse response = httpclient.execute(httpget); 
    HttpEntity entity = response.getEntity(); 

    System.out.println("Login form get: " + response.getStatusLine()); 
    if (entity != null) { 
     entity.consumeContent(); 
    } 
    System.out.println("Initial set of cookies:"); 
    List<Cookie> cookies = httpclient.getCookieStore().getCookies(); 
    if (cookies.isEmpty()) { 
     System.out.println("None"); 
    } else { 
     for (int i = 0; i < cookies.size(); i++) { 
      System.out.println("- " + cookies.get(i).toString()); 
     } 
    } 

    HttpPost httpost = new HttpPost("https://login.yahoo.com/config/login_verify2?.intl=us&.src=ym"); 

    List <NameValuePair> nvps = new ArrayList <NameValuePair>(); 
    nvps.add(new BasicNameValuePair("IDToken1", "Yahoo! ID")); 
    nvps.add(new BasicNameValuePair("IDToken2", "Password")); 

    httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); 

    response = httpclient.execute(httpost); 

    System.out.println("Response "+response.toString()); 
    entity = response.getEntity(); 

    System.out.println("Login form get: " + response.getStatusLine()); 
    if (entity != null) { 

     InputStream is = entity.getContent(); 
     BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
     String str =""; 
     while ((str = br.readLine()) != null){ 
      System.out.println(""+str); 
     } 
    } 

    System.out.println("Post logon cookies:"); 
    cookies = httpclient.getCookieStore().getCookies(); 
    if (cookies.isEmpty()) { 
     System.out.println("None"); 
    } else { 
     for (int i = 0; i < cookies.size(); i++) { 
      System.out.println("- " + cookies.get(i).toString()); 
     } 
    } 
    httpclient.getConnectionManager().shutdown();   
    } 
} 

當我打印從HttpEntity它打印的登錄頁面內容的輸出。如何在使用HttpClient登錄後獲取頁面內容?

回答

2

如果您看到yahoo登錄源頁面,您會看到有很多其他參數沒有在您的請求中發送。

<input type="hidden" name=".tries" value="1"> 
<input type="hidden" name=".src" value="fpctx"> 
<input type="hidden" name=".md5" value=""> 
<input type="hidden" name=".hash" value=""> 
<input type="hidden" name=".js" value=""> 
<input type="hidden" name=".last" value=""> 
<input type="hidden" name="promo" value=""> 
<input type="hidden" name=".intl" value="us"> 
<input type="hidden" name=".bypass" value=""> 
<input type="hidden" name=".partner" value=""> 
<input type="hidden" name=".u" value="a0bljsd77uima"> 
<input type="hidden" name=".v" value="0"> 
<input type="hidden" name=".challenge" value="sCm6Z8Bv1vy78LBlEd8dnFsmbit1"> 
<input type="hidden" name=".yplus" value=""> 
... 

我想這就是雅虎瞭解登錄失敗並將您重新發送到登錄頁面的原因。該登錄頁面就是您所看到的迴應。

許多網站嘗試避免程序化登錄(以避免機器人或其他安全問題),因此可能很難做你正在嘗試。您可以:

  • 如果可能,請使用官方的雅虎公共API。
  • 嘗試使用模擬用戶瀏覽的其他Java庫(如HTTPUnitHtmlUnit,還有許多其他庫),並假冒用戶,彷彿他在瀏覽雅虎頁面一樣。
+0

很好。謝謝。我使用HtmlUnit,它的工作原理,但我得到了 javax.net.ssl.SSLPeerUnverifiedException:對方未驗證 我該如何解決這個錯誤? – Heyy

+0

您正在向https端點發布一些數據,因此您應該配置SSL。我不確定如何使用HtmlUnit,如果它不起作用,您可以嘗試使用webClient.setUseInsecureSSL(true)或google進行該錯誤。 –