Jsoup工具类

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 请求地址
* @param map 请求参数
* @param cookie 请求cookie
* @return
* @throws IOException
*/
public static Document httpPost(String url,Map<String,String> map,String cookie) throws IOException{
//获取请求连接
trustEveryone();
Connection con = Jsoup.connect(url);
//遍历生成参数
if(map!=null){
for (Entry<String, String> entry : map.entrySet()) {
//添加参数
con.data(entry.getKey(), entry.getValue());
}
}
//插入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("Accept-Encoding", "gzip, deflate");
con.header("Cookie", cookie);
con.header("Accept-Charset", "UTF-8");
con.header("referer","https://www.nm.zsks.cn/ptgxzs/xxcx/");

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.post();
System.out.println(doc);
return doc;
}



/**
* 发送post请求获取返回头文件获这cookie
* @param url 请求地址
* @param map 请求参数
* @param cookie 请求cookie
* @param header 返回获取cookie或者头文件
* @return
* @throws IOException
*/
public static String httpPost(String url,Map<String,String> map,String cookie,String header) throws IOException{
//获取请求连接
Connection con = Jsoup.connect(url);
//遍历生成参数
if(map!=null){
for (Entry<String, String> entry : map.entrySet()) {
//添加参数
con.data(entry.getKey(), entry.getValue());
}
}
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))");
//插入cookie(头文件形式)
con.header("Cookie", cookie);
//发送请求
Response resp=con.method(Method.POST).execute();
//获取cookie名称为__bsi的值
String cookieValue = resp.cookie(header);
System.out.println(cookieValue);
return cookieValue;
}

public static String httpPostWithjson(String url, String json) throws IOException {
String result = "";
HttpPost httpPost = new HttpPost(url);
CloseableHttpClient httpClient = HttpClients.createDefault();
try {
BasicResponseHandler handler = new BasicResponseHandler();
StringEntity entity = new StringEntity(json, "utf-8");//解决中文乱码问题
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
result = httpClient.execute(httpPost, handler);
return result;
} catch (Exception e) {
e.printStackTrace();

} finally {
try {
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
public static JSONObject getJsonObject(String url) throws IOException {
JSONObject jsonObject = null;


HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpParams httpParams = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
HttpResponse response = httpClient.execute(httpGet);
StringBuilder builder = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent(), "utf-8"));
for (String s = bufferedReader.readLine(); s != null; s = bufferedReader
.readLine()) {
builder.append(s); } jsonObject =

JSONObject.fromObject(builder.toString());

return jsonObject; }}




posted on 2020-07-27 17:23  TheKingJames  阅读(162)  评论(0编辑  收藏  举报