10 changed files with 485 additions and 106 deletions
@ -0,0 +1,276 @@
|
||||
package com.biutag.supervision.util; |
||||
|
||||
import lombok.experimental.UtilityClass; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.apache.http.Header; |
||||
import org.apache.http.NameValuePair; |
||||
import org.apache.http.client.config.CookieSpecs; |
||||
import org.apache.http.client.config.RequestConfig; |
||||
import org.apache.http.client.entity.UrlEncodedFormEntity; |
||||
import org.apache.http.client.methods.CloseableHttpResponse; |
||||
import org.apache.http.client.methods.HttpDelete; |
||||
import org.apache.http.client.methods.HttpGet; |
||||
import org.apache.http.client.methods.HttpPost; |
||||
import org.apache.http.client.utils.URIBuilder; |
||||
import org.apache.http.conn.ssl.NoopHostnameVerifier; |
||||
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; |
||||
import org.apache.http.conn.ssl.TrustStrategy; |
||||
import org.apache.http.entity.ContentType; |
||||
import org.apache.http.entity.StringEntity; |
||||
import org.apache.http.impl.client.CloseableHttpClient; |
||||
import org.apache.http.impl.client.HttpClients; |
||||
import org.apache.http.message.BasicNameValuePair; |
||||
import org.apache.http.ssl.SSLContextBuilder; |
||||
import org.apache.http.util.EntityUtils; |
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import javax.net.ssl.HostnameVerifier; |
||||
import javax.net.ssl.SSLContext; |
||||
import java.io.UnsupportedEncodingException; |
||||
import java.net.URI; |
||||
import java.net.URISyntaxException; |
||||
import java.security.KeyManagementException; |
||||
import java.security.KeyStoreException; |
||||
import java.security.NoSuchAlgorithmException; |
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @author kami |
||||
* @since 21:48 2020/10/19 |
||||
*/ |
||||
@Slf4j |
||||
@UtilityClass |
||||
public class HttpClientUtil { |
||||
|
||||
|
||||
public static String doGet(String url, Map<String, String> param, Map<String, String> header) { |
||||
long time = System.currentTimeMillis(); |
||||
URIBuilder builder; |
||||
HttpGet httpGet; |
||||
RequestConfig requestConfig = RequestConfig.custom() |
||||
.setCookieSpec(CookieSpecs.STANDARD) |
||||
.setConnectionRequestTimeout(120000) |
||||
.setSocketTimeout(120000) |
||||
.setConnectTimeout(120000).build(); |
||||
try { |
||||
builder = new URIBuilder(url); |
||||
for (Map.Entry<String, String> entry : param.entrySet()) { |
||||
builder.addParameter(entry.getKey(), entry.getValue()); |
||||
} |
||||
URI uri = builder.build(); |
||||
httpGet = new HttpGet(uri); |
||||
httpGet.setConfig(requestConfig); |
||||
for (Map.Entry<String, String> entry : header.entrySet()) { |
||||
httpGet.setHeader(entry.getKey(), entry.getValue()); |
||||
} |
||||
|
||||
} catch (URISyntaxException e) { |
||||
e.printStackTrace(); |
||||
return null; |
||||
} |
||||
try { |
||||
SslUtil.ignoreSsl(); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
// 创建Httpclient对象
|
||||
try (CloseableHttpClient httpclient = createSSLClientDefault(); |
||||
CloseableHttpResponse response = httpclient.execute(httpGet)) { |
||||
// 判断返回状态是否为200
|
||||
if (response.getStatusLine().getStatusCode() == 200) { |
||||
log.info("{}ms", System.currentTimeMillis() - time); |
||||
return EntityUtils.toString(response.getEntity(), "UTF-8"); |
||||
} |
||||
log.info("{}", response.getStatusLine().getStatusCode()); |
||||
return null; |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
public static CloseableHttpClient createSSLClientDefault() { |
||||
try { |
||||
//使用 loadTrustMaterial() 方法实现一个信任策略,信任所有证书
|
||||
// 信任所有
|
||||
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (TrustStrategy) (chain, authType) -> true).build(); |
||||
//NoopHostnameVerifier类: 作为主机名验证工具,实质上关闭了主机名验证,它接受任何
|
||||
//有效的SSL会话并匹配到目标主机。
|
||||
HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE; |
||||
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier); |
||||
return HttpClients.custom().setSSLSocketFactory(sslsf).build(); |
||||
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
return HttpClients.createDefault(); |
||||
|
||||
} |
||||
|
||||
public static Boolean doDelete(String url, Map<String, String> header) { |
||||
URIBuilder builder; |
||||
HttpDelete httpGet; |
||||
try { |
||||
builder = new URIBuilder(url); |
||||
URI uri = builder.build(); |
||||
httpGet = new HttpDelete(uri); |
||||
for (Map.Entry<String, String> entry : header.entrySet()) { |
||||
httpGet.setHeader(entry.getKey(), entry.getValue()); |
||||
} |
||||
} catch (URISyntaxException e) { |
||||
log.error("exception message", e); |
||||
return null; |
||||
} |
||||
// 创建Httpclient对象
|
||||
try (CloseableHttpClient httpclient = HttpClients.createDefault(); |
||||
CloseableHttpResponse response = httpclient.execute(httpGet)) { |
||||
// 判断返回状态是否为200
|
||||
return response.getStatusLine().getStatusCode() == 200; |
||||
} catch (Exception e) { |
||||
log.error("exception message", e); |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
public static String doGetResponseHead(String url,Map<String, String> header,String headKey) { |
||||
URIBuilder builder; |
||||
HttpGet httpGet; |
||||
try { |
||||
builder = new URIBuilder(url); |
||||
URI uri = builder.build(); |
||||
httpGet = new HttpGet(uri); |
||||
for (Map.Entry<String, String> entry : header.entrySet()) { |
||||
httpGet.setHeader(entry.getKey(), entry.getValue()); |
||||
} |
||||
} catch (URISyntaxException e) { |
||||
log.error("exception message", e); |
||||
return null; |
||||
} |
||||
// 创建Httpclient对象
|
||||
try (CloseableHttpClient httpclient = HttpClients.createDefault(); |
||||
CloseableHttpResponse response = httpclient.execute(httpGet)) { |
||||
// 判断返回状态是否为200
|
||||
if (response.getStatusLine().getStatusCode() == 200) { |
||||
return response.getFirstHeader(headKey).getValue(); |
||||
} |
||||
return null; |
||||
} catch (Exception e) { |
||||
log.error("exception message", e); |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
public static String doGet(String url, Map<String, String> header) { |
||||
return doGet(url, new HashMap<>(0), header); |
||||
} |
||||
|
||||
public static String doPost(String url, Map<String, String> param, Map<String, String> headers) { |
||||
HttpPost httpPost = new HttpPost(url); |
||||
RequestConfig requestConfig = RequestConfig.custom() |
||||
.setCookieSpec(CookieSpecs.STANDARD) |
||||
.setConnectionRequestTimeout(120000) |
||||
.setSocketTimeout(120000) |
||||
.setConnectTimeout(120000).build(); |
||||
httpPost.setConfig(requestConfig); |
||||
// 创建参数列表
|
||||
List<NameValuePair> paramList = new ArrayList<>(); |
||||
for (Map.Entry<String, String> entry : param.entrySet()) { |
||||
paramList.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); |
||||
} |
||||
if(!paramList.isEmpty()) { |
||||
try { |
||||
httpPost.setEntity(new UrlEncodedFormEntity(paramList)); |
||||
} catch (UnsupportedEncodingException e) { |
||||
log.error("exception message", e); |
||||
return null; |
||||
} |
||||
} |
||||
for (Map.Entry<String, String> entry : headers.entrySet()) { |
||||
httpPost.setHeader(entry.getKey(), entry.getValue()); |
||||
} |
||||
// 创建Httpclient对象
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault(); |
||||
CloseableHttpResponse response = httpClient.execute(httpPost)) { |
||||
return EntityUtils.toString(response.getEntity(), "utf-8"); |
||||
} catch (Exception e) { |
||||
log.error("exception message", e); |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
public static String doPostJson(String url, String json, Map<String, String> map) { |
||||
long time = System.currentTimeMillis(); |
||||
HttpPost httpPost = new HttpPost(url); |
||||
RequestConfig requestConfig = RequestConfig.custom() |
||||
.setCookieSpec(CookieSpecs.STANDARD) |
||||
.setConnectionRequestTimeout(120000) |
||||
.setSocketTimeout(120000) |
||||
.setConnectTimeout(120000).build(); |
||||
httpPost.setConfig(requestConfig); |
||||
for (Map.Entry<String, String> entry : map.entrySet()) { |
||||
httpPost.setHeader(entry.getKey(), entry.getValue()); |
||||
} |
||||
log.info("请求body: {}",json); |
||||
httpPost.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON)); |
||||
// 创建Httpclient对象
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault(); |
||||
CloseableHttpResponse response = httpClient.execute(httpPost)) { |
||||
log.info("{}ms", System.currentTimeMillis() - time); |
||||
return EntityUtils.toString(response.getEntity(), "utf-8"); |
||||
} catch (Exception e) { |
||||
log.error("exception message", e); |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
public static String doPostJsonCmcc(String url, String json, HttpHeaders headers) { |
||||
long time = System.currentTimeMillis(); |
||||
HttpPost httpPost = new HttpPost(url); |
||||
|
||||
// httpPost.setHeader("Authorization", map.get("Authorization"));
|
||||
// httpPost.setHeader("X-Req-Time", map.get("X-Req-Time"));
|
||||
// httpPost.setHeader("X-Req-Version", map.get("X-Req-Version"));
|
||||
// httpPost.setHeader("X-Req-AppId", map.get("X-Req-AppId"));
|
||||
// httpPost.setHeader("X-Req-ClientIP", map.get("X-Req-ClientIP"));
|
||||
// httpPost.setHeader("X-Req-SignType", map.get("X-Req-SignType"));
|
||||
// httpPost.setHeader("X-Req-Seq", map.get("X-Req-Seq"));
|
||||
// httpPost.setHeader("Content-Type", "application/json");
|
||||
// httpPost.setHeader("X-Req-Hmac", map.get("X-Req-Hmac"));
|
||||
for (Header header : httpPost.getAllHeaders()) { |
||||
System.out.println(header.getName() + ": " + header.getValue()); |
||||
} |
||||
log.info(json); |
||||
httpPost.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON)); |
||||
// 创建Httpclient对象
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault(); |
||||
CloseableHttpResponse response = httpClient.execute(httpPost)) { |
||||
|
||||
log.info("{}ms", System.currentTimeMillis() - time); |
||||
return EntityUtils.toString(response.getEntity(), "utf-8"); |
||||
} catch (Exception e) { |
||||
log.error("exception message", e); |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
public static Boolean doPostJsonEmpty(String url, String json, Map<String, String> map) { |
||||
HttpPost httpPost = new HttpPost(url); |
||||
for (Map.Entry<String, String> entry : map.entrySet()) { |
||||
httpPost.setHeader(entry.getKey(), entry.getValue()); |
||||
} |
||||
httpPost.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON)); |
||||
// 创建Httpclient对象
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault(); |
||||
CloseableHttpResponse response = httpClient.execute(httpPost)) { |
||||
if (response.getStatusLine().getStatusCode() == 200) { |
||||
return true; |
||||
} |
||||
} catch (Exception e) { |
||||
log.error("exception message", e); |
||||
} |
||||
return false; |
||||
} |
||||
} |
||||
@ -0,0 +1,64 @@
|
||||
package com.biutag.supervision.util; |
||||
|
||||
import lombok.experimental.UtilityClass; |
||||
|
||||
import javax.net.ssl.*; |
||||
import java.security.cert.CertificateException; |
||||
import java.security.cert.X509Certificate; |
||||
|
||||
/** |
||||
* @author kami on 2024-02-07 11:17:09 |
||||
* @version 0.0.1 |
||||
* @since 1.8 |
||||
*/ |
||||
@UtilityClass |
||||
public class SslUtil { |
||||
|
||||
private static void trustAllHttpsCertificates() throws Exception { |
||||
TrustManager[] trustAllCerts = new TrustManager[1]; |
||||
TrustManager tm = new miTM(); |
||||
trustAllCerts[0] = tm; |
||||
SSLContext sc = SSLContext.getInstance("SSL"); |
||||
sc.init(null, trustAllCerts, null); |
||||
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); |
||||
} |
||||
|
||||
static class miTM implements TrustManager, X509TrustManager { |
||||
public X509Certificate[] getAcceptedIssuers() { |
||||
return null; |
||||
} |
||||
|
||||
public boolean isServerTrusted(X509Certificate[] certs) { |
||||
return true; |
||||
} |
||||
|
||||
public boolean isClientTrusted(X509Certificate[] certs) { |
||||
return true; |
||||
} |
||||
|
||||
public void checkServerTrusted(X509Certificate[] certs, String authType) |
||||
throws CertificateException { |
||||
return; |
||||
} |
||||
|
||||
public void checkClientTrusted(X509Certificate[] certs, String authType) |
||||
throws CertificateException { |
||||
return; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 忽略HTTPS请求的SSL证书,必须在openConnection之前调用 |
||||
* @throws Exception |
||||
*/ |
||||
public static void ignoreSsl() throws Exception{ |
||||
HostnameVerifier hv = new HostnameVerifier() { |
||||
public boolean verify(String urlHostName, SSLSession session) { |
||||
System.out.println("Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost()); |
||||
return true; |
||||
} |
||||
}; |
||||
trustAllHttpsCertificates(); |
||||
HttpsURLConnection.setDefaultHostnameVerifier(hv); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue