2011-10-11 76 views
5
public boolean isGood(String path) 
{ 
    if (p != path) 
    { 
     good = false; 
    } 

    if (good) 
    { 
     try 
     { 
      Connection connection = Jsoup.connect(path); 
      Map<String, String> cookys = Jsoup.connect(path).response().cookies(); 

      if (cookys != cookies) 
       cookies = cookys; 

      for (Entry<String, String> cookie : cookies.entrySet()) 
      { 
       connection.cookie(cookie.getKey(), cookie.getValue()); 
      } 

      Doc = connection.get(); 
      good = true; 
     } 
     catch (Exception e) 
     { 
      rstring = e.getMessage().toString(); 
      good = false; 
     } 
    } 
    else 
    { 
     try 
     { 
      Response response = Jsoup.connect(path).execute(); 
      cookies = response.cookies(); 
      Doc = response.parse(); 
      good = true; 
     } 
     catch (Exception e) 
     { 
      rstring = e.getMessage().toString(); 
      good = false; 
     } 
    }  
    return good; 
} 

此方法不正確。我想知道的是一種不知道cookie會存在的方式,能夠處理cookie更改以及維護會話。如何使用jsoup維護可變的cookie和會話?

我正在爲我的簡單機器論壇編寫一個應用程序,並在您點擊某些自定義行爲時更改其Cookie配置。

但是,如果應用程序對我的網站很好,我將發佈一個版本供其他論壇使用。

我知道我正朝着正確的方向前進,但邏輯有點踢我的屁股。

任何意見將不勝感激。

+0

除了BalusC的意見,這是不可能的'p!=路徑'事情是你真正的意思,儘管它是*可能*。 –

回答

12

這段代碼很混亂。流程不合邏輯,異常處理不好。像if (p != path)if (cookys != cookies)這樣的對象參考比較沒有任何意義。要比較對象的內容,您需要改爲使用equals()方法。

至此,我明白你希望在同一個域上的一堆隨後的Jsoup請求中維護cookie。在這種情況下,你需要基本上堅持以下流程:

Map<String, String> cookies = new HashMap<String, String>(); 

// First request. 
Connection connection1 = Jsoup.connect(url1); 
for (Entry<String, String> cookie : cookies.entrySet()) { 
    connection1.cookie(cookie.getKey(), cookie.getValue()); 
} 
Response response1 = connection1.execute(); 
cookies.putAll(response1.cookies()); 
Document document1 = response1.parse(); 
// ... 

// Second request. 
Connection connection2 = Jsoup.connect(url2); 
for (Entry<String, String> cookie : cookies.entrySet()) { 
    connection2.cookie(cookie.getKey(), cookie.getValue()); 
} 
Response response2 = connection2.execute(); 
cookies.putAll(response2.cookies()); 
Document document2 = response2.parse(); 
// ... 

// Third request. 
Connection connection3 = Jsoup.connect(url3); 
for (Entry<String, String> cookie : cookies.entrySet()) { 
    connection3.cookie(cookie.getKey(), cookie.getValue()); 
} 
Response response3 = connection3.execute(); 
cookies.putAll(response3.cookies()); 
Document document3 = response3.parse(); 
// ... 

// Etc. 

這可重構以下方法:

private Map<String, String> cookies = new HashMap<String, String>(); 

public Document get(url) throws IOException { 
    Connection connection = Jsoup.connect(url); 
    for (Entry<String, String> cookie : cookies.entrySet()) { 
     connection.cookie(cookie.getKey(), cookie.getValue()); 
    } 
    Response response = connection.execute(); 
    cookies.putAll(response.cookies()); 
    return response.parse(); 
} 

可以用來作爲

YourJsoupWrapper jsoupWrapper = new YourJsoupWrapper(); 

Document document1 = jsoupWrapper.get(url1); 
// ... 

Document document2 = jsoupWrapper.get(url2); 
// ... 

Document document3 = jsoupWrapper.get(url3); 
// ... 

請注意,即將推出的Jsoup 1.6.2將附帶一個新的Connection#cookies(Map)方法,該方法應該使for每次循環多餘。

+0

我非常感謝你。我搜索並搜索了一個正確的做法。這種方式肯定比我嘗試的方式更好。大聲笑 – texasman1979

+0

來自未來的人!出於某種原因,我發現JSoup非常方便。只是我的兩分錢就是在當前JSoup庫中有一個名爲cookies(Map cookies)的方法,它添加了鍵/值對。所以上面的foreach可以替換爲:connection.cookies(cookies) –

+0

你可以做connection.cookies(cookies)而不是for循環 – caub

1

+1 BalusC

我在你的代碼改變了一些,它爲我的作品,讓你從網站得到的cookie,只比獲得文檔

public Document get(String url) throws IOException { 
    Connection connection = Jsoup.connect(url).userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"); 
    Connection.Response response = connection.execute(); 
    connection.cookies(response.cookies()); 
    return connection.get(); 
}