日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java jsoup获取cookie_java – 如何使用jsoup维护变量cookie和会话?

發(fā)布時間:2024/7/5 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java jsoup获取cookie_java – 如何使用jsoup维护变量cookie和会话? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

這段代碼非常令人困惑.流程不合邏輯,異常處理很糟糕.像if(p!= path)和if(cookys!= cookies)之類的對象引用比較沒有任何意義.要比較對象的內(nèi)容,您需要使用equals()方法.

到目前為止,我知道您希望在同一個域上的一堆后續(xù)Jsoup請求中維護(hù)cookie.在這種情況下,您需要基本遵循以下流程:

Map cookies = new HashMap();

// First request.

Connection connection1 = Jsoup.connect(url1);

for (Entry 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 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 cookie : cookies.entrySet()) {

connection3.cookie(cookie.getKey(), cookie.getValue());

}

Response response3 = connection3.execute();

cookies.putAll(response3.cookies());

Document document3 = response3.parse();

// ...

// Etc.

這可以重構(gòu)為以下方法:

private Map cookies = new HashMap();

public Document get(url) throws IOException {

Connection connection = Jsoup.connect(url);

for (Entry 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)方法,該方法應(yīng)該使循環(huán)每次都是多余的.

總結(jié)

以上是生活随笔為你收集整理的java jsoup获取cookie_java – 如何使用jsoup维护变量cookie和会话?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。