Maven依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.3</version>
</dependency>
工具类
public class HttpUtils {
private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(3000).setConnectTimeout(3000)
.build();
/***
* Https
* @return 返回对象
*/
public static CloseableHttpClient createSSLClientDefault() {
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
// 信任所有
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
}
return HttpClients.createDefault();
}
/**
* Get请求
* @param url 请求地址
* @param mapData 携带参数
* @return 返回Response 数据
* @throws ClientProtocolException 发送的异常
* @throws IOException IO异常
* @throws URISyntaxException URI 异常
*/
public HttpResponse doGet(String url, Map<String, String> mapData,Map<String, String> header)
throws ClientProtocolException, IOException, URISyntaxException {
CloseableHttpResponse response = null;
HttpGet hg = new HttpGet();
hg.setConfig(requestConfig);
URIBuilder ub = new URIBuilder(url);
//CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpClient client = HttpUtils.createSSLClientDefault();
if (header!=null&&header.size()>0) {
for (Map.Entry<String, String> e : header.entrySet()) {
hg.setHeader(e.getKey(),e.getValue());
}
}
if (mapData != null&&mapData.size()>0) {
for (Map.Entry<String, String> e : mapData.entrySet()) {
ub.addParameter(e.getKey(), e.getValue());
}
}
hg.setURI(ub.build());
response = client.execute(hg);
return response;
}
public HttpResponse doPost(String url,Map<String, String> mapData) throws ClientProtocolException, IOException {
CloseableHttpResponse response = null;
HttpPost hp=new HttpPost(url);
hp.setConfig(requestConfig);
CloseableHttpClient client = HttpClients.createDefault();
hp.setHeader("Content-type", "application/json; charset=utf-8");
if (mapData!=null&&mapData.size()>0) {
String jsonData= JSONObject.toJSONString(mapData);
StringEntity postEntity=new StringEntity(jsonData,"utf-8");
postEntity.setContentType("application/json");
hp.setEntity(postEntity);
}
response=client.execute(hp);
return response;
}
public HttpResponse doPostHttps(String url,Map<String, String> mapData) throws ClientProtocolException, IOException {
CloseableHttpResponse response = null;
HttpPost hp=new HttpPost(url);
hp.setConfig(requestConfig);
CloseableHttpClient client = HttpUtils.createSSLClientDefault();
hp.setHeader("Content-type", "application/json; charset=utf-8");
if (mapData!=null&&mapData.size()>0) {
String jsonData= JSONObject.toJSONString(mapData);
StringEntity postEntity=new StringEntity(jsonData,"utf-8");
postEntity.setContentType("application/json");
hp.setEntity(postEntity);
}
response=client.execute(hp);
return response;
}