package com.thhy.usercore.utils;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.thhy.general.common.BasicMessage;
|
import com.thhy.general.common.BasicStatus;
|
import com.thhy.general.exception.BasicException;
|
import jdk.nashorn.internal.ir.ObjectNode;
|
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpStatus;
|
import org.apache.http.client.ClientProtocolException;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.entity.StringEntity;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.HttpClientBuilder;
|
import org.apache.http.util.EntityUtils;
|
|
import java.io.ByteArrayOutputStream;
|
import java.io.IOException;
|
import java.io.UnsupportedEncodingException;
|
|
public class HttpUtils {
|
|
/**
|
* get请求
|
* @param url
|
* @return
|
*/
|
public static String get(String url) {
|
try {
|
CloseableHttpClient client = HttpClientBuilder.create().build();
|
HttpGet request = new HttpGet(url);
|
HttpResponse response = client.execute(request);
|
int code = response.getStatusLine().getStatusCode();
|
//logger.info("请求URL:" + url + ";code:"+ code);
|
if (code == HttpStatus.SC_OK) {
|
String result = EntityUtils.toString(response.getEntity());
|
return result;
|
}
|
}
|
catch (IOException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
public static String get(CloseableHttpClient httpClient,String url) {
|
try {
|
HttpGet request = new HttpGet(url);
|
request.addHeader("Accept", "application/json");
|
request.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
|
|
HttpResponse response = httpClient.execute(request);
|
int code = response.getStatusLine().getStatusCode();
|
//logger.info("请求URL:" + url + ";code:"+ code);
|
String result = EntityUtils.toString(response.getEntity());
|
return result;
|
}
|
catch (IOException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
public static String post(CloseableHttpClient httpClient, String url, ObjectMapper objectMapper, ObjectNode rootNode){
|
|
// 创建 HttpPost 请求
|
HttpPost httpPost = new HttpPost(url);
|
// 设置长连接
|
httpPost.setHeader("Connection", "keep-alive");
|
httpPost.addHeader("Accept", "application/json");
|
httpPost.addHeader("Content-type","application/json; charset=utf-8");
|
|
// 设置代理(模拟浏览器版本)
|
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
|
// 设置 Cookie
|
httpPost.setHeader("Cookie", "UM_distinctid=16442706a09352-0376059833914f-3c604504-1fa400-16442706a0b345; CNZZDATA1262458286=1603637673-1530123020-%7C1530123020; JSESSIONID=805587506F1594AE02DC45845A7216A4");
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
CloseableHttpResponse response = null;
|
try {
|
// 设置 HttpPost 参数
|
objectMapper.writeValue(bos, rootNode);
|
|
httpPost.setEntity(new StringEntity(bos.toString("UTF-8"), "UTF-8"));
|
response = httpClient.execute(httpPost);
|
|
String bodyAsString = EntityUtils.toString(response.getEntity());
|
System.out.println(bodyAsString);
|
return bodyAsString;
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
} catch (ClientProtocolException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
} finally {// 无论如何必须关闭连接
|
try {
|
if (response != null) {
|
response.close();
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
|
try {
|
if (httpClient != null) {
|
httpClient.close();
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return "";
|
}
|
|
public static String normalpost(String url, ObjectMapper objectMapper,ObjectNode rootNode){
|
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
|
// 创建 HttpPost 请求
|
HttpPost httpPost = new HttpPost(url);
|
// 设置长连接
|
httpPost.setHeader("Connection", "keep-alive");
|
httpPost.addHeader("Accept", "application/json");
|
httpPost.addHeader("Content-type","application/json; charset=utf-8");
|
|
// 设置代理(模拟浏览器版本)
|
httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
|
// 设置 Cookie
|
httpPost.setHeader("Cookie", "UM_distinctid=16442706a09352-0376059833914f-3c604504-1fa400-16442706a0b345; CNZZDATA1262458286=1603637673-1530123020-%7C1530123020; JSESSIONID=805587506F1594AE02DC45845A7216A4");
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
CloseableHttpResponse response = null;
|
try {
|
// 设置 HttpPost 参数
|
objectMapper.writeValue(bos, rootNode);
|
|
httpPost.setEntity(new StringEntity(bos.toString("UTF-8"), "UTF-8"));
|
response = httpClient.execute(httpPost);
|
|
String bodyAsString = EntityUtils.toString(response.getEntity());
|
System.out.println(bodyAsString);
|
return bodyAsString;
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
} catch (ClientProtocolException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
throw new BasicException(BasicStatus.ERROR);
|
} finally {// 无论如何必须关闭连接
|
try {
|
if (response != null) {
|
response.close();
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
|
try {
|
if (httpClient != null) {
|
httpClient.close();
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return "";
|
}
|
}
|