Cookie管理Java
我正在尝试获取所有的cookies信息,同时为网站进行网络爬网。到目前为止,我已经编写了这段代码Cookie管理Java,java,web-scraping,jsoup,Java,Web Scraping,Jsoup,我正在尝试获取所有的cookies信息,同时为网站进行网络爬网。到目前为止,我已经编写了这段代码 Map<String, String> cookies = response.cookies(); 另外,我正在使用Jsoup请求和响应。能否提供用于发送请求的代码,包括请求头?@ShafinMahmud我已经上传了代码。 public static Connection makeRequest(UrlProperty urlProperty) { Response
Map<String, String> cookies = response.cookies();
另外,我正在使用Jsoup请求和响应。能否提供用于发送请求的代码,包括请求头?@ShafinMahmud我已经上传了代码。
public static Connection makeRequest(UrlProperty urlProperty) {
Response lastResponse = urlProperty.getReponse();
Connection connection = Jsoup.connect(urlProperty.getName());
connection.userAgent(Constants.USER_AGENT);
connection.timeout(Constants.TIME_OUT);
if(lastResponse != null) {
Map<String, String> cookies = lastResponse.cookies();
connection.cookies(cookies);
String cookieString = "";
for (Map.Entry<String, String> cookie : cookies.entrySet()) {
cookieString = cookie.getKey() + "=" + cookie.getValue() + "; ";
}
if(!cookieString.isEmpty()) {
connection.request().header("Cookie", cookieString);
}
}
return connection;
}