2015-09-25 39 views
0

最近我試圖讓應用程序關注特定的網站。我需要訪問登錄後具有可見內容的頁面。如果我明白,下面的代碼顯示,首先我需要連接到url1以避免默認主頁,所以在這裏我無法發送數據(登錄名,密碼)。我需要看到來自url3的內容,但在這裏我也無法發送數據,因爲沒有登錄名和密碼字段。他們在url2中。我嘗試了本網站的其他解決方案,但我只收到每個人都可以看到的內容。任何人都可以幫忙嗎?發送數據需要查看來自網站的隱藏內容

private class Parser extends AsyncTask<Void, Void, Void> { 
    String h; 
    String url1 = "http://www.klt.net.pl/"; 
    String url2 = "http://www.klt.net.pl/index.php?a=logowanie"; 
    String url3 = "http://www.klt.net.pl/index.php?a=przedmecz1&b=2&d=2038"; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pd = new ProgressDialog(MainActivity.this); 
     pd.setTitle("Parser"); 
     pd.setMessage("Loading..."); 
     pd.setIndeterminate(false); 
     pd.show(); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     try { 
      Connection.Response response = Jsoup.connect(url1) 
      .method(Connection.Method.GET) 
      .timeout(50000) 
      .followRedirects(true) 
      .execute(); 
     Document document = Jsoup.connect(url2) 
      .cookies(response.cookies()) 
      .get(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     pd.dismiss(); 
    } 
} 

編輯:

@Override 
protected Void doInBackground(Void... params) { 
    try { 
     Connection.Response response = Jsoup.connect(url1) 
     .method(Connection.Method.GET) 
     .timeout(50000) 
     .followRedirects(true) 
     .execute(); 

     Connection.Response loginRes = Jsoup.connect(url2) 
     .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36") 
     .data("login", getlog2, 
     "haslo", getpass2) 
     .cookies(response.cookies()) 
     .method(Method.POST) 
     .execute(); 

     Map<String, String> cookies = new Map<String, String>(); 
     cookies.addAll(loginRes.cookies()); 

     Connection.Response otherRes = Jsoup.connect(url3) 
     .cookies(cookies) 
     .method(Method.POST) 
     .execute(); 

     d3 = Jsoup.connect(url3) 
     .cookies(otherRes.cookies()) 
     .get(); 

我更新的代碼。這可以嗎?我在Map中有錯誤(無法安裝類型並且無法解析類型)。

+0

你的問題還不是很清楚 –

+0

這可以幫助更好地理解:http://stackoverflow.com/questions/32734928/jsoup-parsing-from-direct-link-doesnt-work/32737661?noredirect = 1#comment53330262_32737661但現在我需要從url3獲取內容。 – zdc

+0

嘿:)這裏可以幫助你解決第二個問題!所以你想通過url2登錄來訪問url3中的內容,這是正確的嗎? –

回答

0

登陸,你需要知道什麼數據POST(ID,密碼,會話cookie等..),以及URL地址需要POST來。

此信息一般都包含在登錄表單,我會解釋這個下面做所需的步驟:你需要輸入的ID密碼

步驟1登錄應該是表單的輸入。因此,只需右鍵單擊您在ID中輸入的區域,然後選擇Inspect Element(假設您使用的是Chrome)。在那裏你將能夠檢查輸入和表單的屬性。

步驟2:密切調查的形式,並保持的紀錄所有輸入欄中(包括隱藏字段)。你需要知道所有領域的namevalue。您還需要知道表單請求是否在GETPOST以及表單的action值中進行。

第3步:現在讓我們來看看有趣的部分。使用以下代碼片段向服務器發出請求並檢索所需的內容。

Connection.Response loginRes = Jsoup.connect(loginUrl) 
           .userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36" 
           .data("login", yourID 
            "haslo", yourPassword) 
           .cookies(response.cookies()) //this is the same cookie you used for url2! 
           .method(Method.POST) 
           .execute(); 
  • loginUrl是請求的地址,而你的情況是"http://www.klt.net.pl/index.php?a=logowanie"
  • userAgent告訴服務器您的瀏覽器的詳細信息。
  • data是你把你的名字&值對的所有輸入字段的形式。
  • cookies是你放置你的cookies的地方,你需要檢查你的請求是否需要cookies被服務器接受,這可以在「cookies」部分的網絡選項卡中檢查。在你的情況下,它是用於url2的同一個cookie。
  • method指定您的請求方法。

檢索到的loginRes對象將包含您需要的所有信息,html,cookie和所有內容。

你已經成功登錄後,請確保您在Map對象存儲cookie的值象下面這樣:

Map<String, String> cookies; 
cookies.putAll(loginRes.cookies()); 

並確保通過這一cookies到餅乾參數今後所有申請,如下所示:

Connection.Response otherRes = Jsoup.connect(otherUrl).cookies(cookies).... 

這將確保您的登錄會話得到維護,並且服務器知道您是經過身份驗證的用戶。

----------------更新------------從doInBackground任務開始

申報地圖的cookie。然後在您提出每個請求後,將所有COOKIES存儲。所以:

cookies = response.cookies(); 
cookies.putAll(loginRes.cookies(); 
cookies.putAll(otherRes.cookies(); 
+0

謝謝你的解釋,但我還有一點問題。請檢查我更新的問題。 – zdc

+0

嗨,看到我更新的答案請 –

+0

我已經嘗試過它,但它給了我錯誤,我必須初始化變量,所以我將它設置爲空,然後它告訴我,變量只能在這個位置爲null。問題比我想象的要困難:/ – zdc