public class JsoupUtil {
/**
* 发送get请求获取返回body
* @param url 请求地址
* @param cookie 请求cookie
* @return
* @throws IOException
*/
public static Document httpGet(String url,String cookie) throws IOException{
//获取请求连接
trustEveryone();
Connection con = Jsoup.connect(url);
//请求头设置,特别是cookie设置
con.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
con.header("Content-Type", "application/x-www-form-urlencoded");
con.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36");
con.header("Cookie", cookie);
con.header("Referer","https://www.nm.zsks.cn/ptgxzs/xxcx/");
con.header("Accept-Charset", "GBK");
con.header("Host", "www1.nm.zsks.cn");
con.header("Cache-Control", "max-age=0");
con.header("Connection", "www1.nm.zsks.cn");
con.header("Upgrade-Insecure-Requests", "1");
//解析请求结果
//Document doc=con.get(); //数据正常获取
Document doc = Jsoup.parse(new URL(url).openStream(), "UTF-8", url); //网报统计用 需要转编码
//获取标题
System.out.println(doc);
return doc;
}
/**
* 发送get请求获取返回headers具体值
* @param url 请求地址
* @param cookie 请求cookie
* @return
* @throws IOException
*/
public static String httpGetHeader(String url,String cook,String header) throws IOException{
//获取请求连接
Connection con = Jsoup.connect(url);
//请求头设置,特别是cookie设置
con.header("Accept", "text/html, application/xhtml+xml, */*");
con.header("Content-Type", "application/x-www-form-urlencoded");
con.header("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0))");
con.header("Cookie", cook);
con.header("referer","https://www.nm.zsks.cn/ptgxzs/xxcx/");
//发送请求
Response resp=con.method(Method.GET).execute();
//获取cookie名称为__bsi的值
String cookieValue = resp.cookie(header);
// System.out.println("cookie __bsi值: "+cookieValue);
// //获取返回cookie所值
// Map<String,String> cookies = resp.cookies();
// System.out.println("所有cookie值: "+cookies);
// //获取返回头文件值
// String headerValue = resp.header(header);
// System.out.println("头文件"+header+"的值:"+headerValue);
// //获取所有头文件值
// Map<String,String> headersOne =resp.headers();
// System.out.println("所有头文件值:"+headersOne);
return cookieValue;
}
public static void trustEveryone() {
try {
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new X509TrustManager[] { new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
} }, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
} catch (Exception e) {
// e.printStackTrace();
}
}
/**
* 发送post请求获取返回body
* @param url 请求地址