2013-04-20 33 views
1

我正在嘗試登錄到以下網站:http://www.deeproute.com。登錄表單字段是這樣:登錄到網站以抓取java中的數據

<input type="hidden" name="cookieexists" value="false"> 
<input size=12 type=name name=name> 
<input size=12 type=password name=password> 
<input type=submit name=subbera value="Login"> 

這裏是我的代碼中,我試圖用HttpClient的登錄和解析與Jsoup生成的HTML。不幸的是,這會返回相同的未登錄狀態下頁面的原始html。

 HttpResponse res = null; 
     Document homePage = null; 
     HttpEntity entity = null; 

     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://www.deeproute.com"); 
     String html = null; 

     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3); 
     nameValuePairs.add(new BasicNameValuePair("cookieexists", "false")); 
     nameValuePairs.add(new BasicNameValuePair("name", username)); 
     nameValuePairs.add(new BasicNameValuePair("password", pass)); 

     try { 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      res = httpclient.execute(httppost); 

     } catch (IOException e) { 

      e.printStackTrace(); 
     } 

     if (res != null) { 

      try { 
       html = EntityUtils.toString(res.getEntity()); 
       homePage = Jsoup.parse(html); 
      } catch (ParseException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

我該怎麼做才能解決這個問題?

回答

1

帶jSoup唯一代碼的工作解決方案。

  • 步驟1.使用登錄表單
  • 步驟2.郵政形式附着餅乾和所有參數。

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

    Connection.Response loginForm = Jsoup.connect("http://deeproute.com/deeproute/default.asp") 
      .method(Connection.Method.GET) 
      .execute(); 

    Document document = Jsoup.connect("http://deeproute.com/deeproute/default.asp") 
      .data("cookieexists", "false") 
      .data("name", "username") 
      .data("password", "pass") 
      .data("subbera", "Login") 
      .cookies(loginForm.cookies()) 
      .post(); 

}