package com.ruoyi.common.utils.huawei;
|
|
import org.apache.http.HttpResponse;
|
import org.apache.http.NameValuePair;
|
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.methods.HttpGet;
|
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpUriRequest;
|
import org.apache.http.client.utils.URIBuilder;
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
import org.apache.http.conn.ssl.SSLContextBuilder;
|
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.DefaultHttpClient;
|
import org.apache.http.impl.client.HttpClients;
|
|
import java.io.IOException;
|
import java.net.URISyntaxException;
|
import java.security.KeyManagementException;
|
import java.security.KeyStoreException;
|
import java.security.NoSuchAlgorithmException;
|
import java.security.cert.CertificateException;
|
import java.security.cert.X509Certificate;
|
import java.util.List;
|
import java.util.Map;
|
|
import javax.net.ssl.HostnameVerifier;
|
import javax.net.ssl.HttpsURLConnection;
|
import javax.net.ssl.SSLContext;
|
import javax.net.ssl.SSLSession;
|
import javax.net.ssl.TrustManager;
|
import javax.net.ssl.X509TrustManager;
|
|
@SuppressWarnings("deprecation")
|
public class HttpsUtil extends DefaultHttpClient {
|
public final static String CONTENT_LENGTH = "Content-Length";
|
|
private static CloseableHttpClient httpClient = getHttpClient();
|
|
public static CloseableHttpClient getHttpClient() {
|
SSLContext sslcontext = null;
|
try {
|
sslcontext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
|
|
@Override
|
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
|
return true;
|
}
|
}).build();
|
} catch (KeyManagementException e) {
|
return null;
|
} catch (NoSuchAlgorithmException e) {
|
return null;
|
} catch (KeyStoreException e) {
|
return null;
|
}
|
|
// Allow TLSv1, TLSv1.1, TLSv1.2 protocol only
|
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,
|
new String[] {"TLSv1", "TLSv1.1", "TLSv1.2"}, null,
|
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
|
CloseableHttpClient httpclient = HttpClients.custom()
|
.setDefaultRequestConfig(RequestConfig.custom().setRedirectsEnabled(false).build())
|
.setSSLSocketFactory(sslsf).build();
|
|
return httpclient;
|
}
|
|
public HostnameVerifier hv = new HostnameVerifier() {
|
public boolean verify(String urlHostName, SSLSession session) {
|
return true;
|
}
|
};
|
|
public 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 java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
return null;
|
}
|
|
public boolean isServerTrusted(X509Certificate[] certs) {
|
return true;
|
}
|
|
public boolean isClientTrusted(X509Certificate[] certs) {
|
return true;
|
}
|
|
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
|
throws java.security.cert.CertificateException {
|
return;
|
}
|
|
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
|
throws java.security.cert.CertificateException {
|
return;
|
}
|
}
|
|
public StreamClosedHttpResponse doPostJsonGetStatusLine(String url, Map<String, String> headerMap, String content) {
|
HttpPost request = new HttpPost(url);
|
addRequestHeader(request, headerMap);
|
|
request.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON));
|
|
HttpResponse response = executeHttpRequest(request);
|
if (null == response) {
|
System.out.println("The response body is null.");
|
}
|
|
return (StreamClosedHttpResponse) response;
|
}
|
|
public HttpResponse doGetWithParas(String url, List<NameValuePair> queryParams, Map<String, String> headerMap)
|
throws Exception {
|
HttpGet request = new HttpGet();
|
addRequestHeader(request, headerMap);
|
|
URIBuilder builder;
|
try {
|
builder = new URIBuilder(url);
|
} catch (URISyntaxException e) {
|
System.out.printf("URISyntaxException: {}", e);
|
throw new Exception(e);
|
}
|
|
if (queryParams != null && !queryParams.isEmpty()) {
|
builder.setParameters(queryParams);
|
}
|
request.setURI(builder.build());
|
|
return executeHttpRequest(request);
|
}
|
|
public StreamClosedHttpResponse doGetWithParasGetStatusLine(String url, List<NameValuePair> queryParams,
|
Map<String, String> headerMap) throws Exception {
|
HttpResponse response = doGetWithParas(url, queryParams, headerMap);
|
if (null == response) {
|
System.out.println("The response body is null.");
|
}
|
|
return (StreamClosedHttpResponse) response;
|
}
|
|
private static void addRequestHeader(HttpUriRequest request, Map<String, String> headerMap) {
|
if (headerMap == null) {
|
return;
|
}
|
|
for (String headerName : headerMap.keySet()) {
|
if (CONTENT_LENGTH.equalsIgnoreCase(headerName)) {
|
continue;
|
}
|
|
String headerValue = headerMap.get(headerName);
|
request.addHeader(headerName, headerValue);
|
}
|
}
|
|
private HttpResponse executeHttpRequest(HttpUriRequest request) {
|
HttpResponse response = null;
|
|
try {
|
response = httpClient.execute(request);
|
} catch (Exception e) {
|
System.out.println("executeHttpRequest failed.");
|
System.out.println(e.getStackTrace());
|
} finally {
|
try {
|
response = new StreamClosedHttpResponse(response);
|
} catch (IOException e) {
|
System.out.println("IOException: " + e.getMessage());
|
}
|
}
|
|
return response;
|
}
|
}
|