|
@@ -0,0 +1,174 @@
|
|
|
|
+package com.kmall.admin.haikong.utils;
|
|
|
|
+
|
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
|
+import org.apache.http.HttpStatus;
|
|
|
|
+import org.apache.http.NameValuePair;
|
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
|
+import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
|
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
|
|
|
+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.util.EntityUtils;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+
|
|
|
|
+import javax.net.ssl.*;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.security.KeyManagementException;
|
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
|
+import java.security.cert.X509Certificate;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Iterator;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+public class HttpsClientUtil {
|
|
|
|
+
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(HttpsClientUtil.class);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private static RequestConfig requestConfig = RequestConfig.custom()
|
|
|
|
+ .setSocketTimeout(5000)
|
|
|
|
+ .setConnectTimeout(5000)
|
|
|
|
+ .setConnectionRequestTimeout(5000)
|
|
|
|
+ .build();//设置请求和传输超时时间
|
|
|
|
+ private final static String charset = "UTF-8";
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 忽略SSL证书校验的POST请求.
|
|
|
|
+ * @param url
|
|
|
|
+ * @return
|
|
|
|
+ * @throws IOException
|
|
|
|
+ */
|
|
|
|
+ public static String post (String url, StringEntity stringEntity){
|
|
|
|
+ HttpResponse res = null;
|
|
|
|
+ try {
|
|
|
|
+ stringEntity.setContentType(ContentType.APPLICATION_JSON.toString());
|
|
|
|
+ res = postnew(url,stringEntity);
|
|
|
|
+ return EntityUtils.toString(res.getEntity(), charset);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ log.error("数据请求失败",e);
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 忽略SSL证书校验的POST请求.
|
|
|
|
+ * @param url
|
|
|
|
+ * @return
|
|
|
|
+ * @throws IOException
|
|
|
|
+ */
|
|
|
|
+ public static String vmcconnectPost(String url, StringEntity stringEntity){
|
|
|
|
+ HttpResponse res = null;
|
|
|
|
+ try {
|
|
|
|
+ stringEntity.setContentType(ContentType.MULTIPART_FORM_DATA.toString());
|
|
|
|
+ res = postnew(url,stringEntity);
|
|
|
|
+ return EntityUtils.toString(res.getEntity(), charset);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ log.error("请求免税MALL接口异常",e);
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 忽略SSL证书校验的POST.
|
|
|
|
+ * @param httpUrl
|
|
|
|
+ * @param map Map
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static String post(String httpUrl, Map<String, Object> map) throws IOException {
|
|
|
|
+ //设置参数
|
|
|
|
+ List<NameValuePair> list = new ArrayList<NameValuePair>();
|
|
|
|
+ Iterator iterator = map.entrySet().iterator();
|
|
|
|
+ while(iterator.hasNext()){
|
|
|
|
+ Map.Entry<String,String> entry = (Map.Entry<String, String>) iterator.next();
|
|
|
|
+ list.add(new BasicNameValuePair(entry.getKey(),String.valueOf(entry.getValue())));
|
|
|
|
+ }
|
|
|
|
+ if(list.size() > 0){
|
|
|
|
+ UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);
|
|
|
|
+ HttpResponse res = null;
|
|
|
|
+ if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
|
|
|
+ return EntityUtils.toString(res.getEntity(), charset);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 忽略SSL证书校验的POST.
|
|
|
|
+ * @param url
|
|
|
|
+ * @param httpEntity HttpEntity
|
|
|
|
+ * @return
|
|
|
|
+ * @throws IOException
|
|
|
|
+ */
|
|
|
|
+ public static HttpResponse post(String url, HttpEntity httpEntity) throws IOException {
|
|
|
|
+ CloseableHttpClient sslClient = createSSLClient();
|
|
|
|
+ HttpPost post = new HttpPost(url);
|
|
|
|
+ post.setEntity(httpEntity);
|
|
|
|
+ post.setConfig(requestConfig);
|
|
|
|
+ HttpResponse response = sslClient.execute(post);
|
|
|
|
+ return response;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 忽略SSL证书校验的POST.
|
|
|
|
+ * @param url
|
|
|
|
+ * @param httpEntity HttpEntity
|
|
|
|
+ * @return
|
|
|
|
+ * @throws IOException
|
|
|
|
+ */
|
|
|
|
+ public static HttpResponse postnew(String url, HttpEntity httpEntity) throws IOException {
|
|
|
|
+ CloseableHttpClient sslClient = createSSLClient();
|
|
|
|
+ HttpPost post = new HttpPost(url);
|
|
|
|
+ post.setEntity(httpEntity);
|
|
|
|
+ post.setConfig(requestConfig);
|
|
|
|
+ HttpResponse response = sslClient.execute(post);
|
|
|
|
+ return response;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 忽略SSL证书校验的CloseableHttpClient.
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static CloseableHttpClient createSSLClient(){
|
|
|
|
+ // 在JSSE中,证书信任管理器类就是实现了接口X509TrustManager的类。我们可以自己实现该接口,让它信任我们指定的证书。
|
|
|
|
+ // 创建SSLContext对象,并使用我们指定的信任管理器初始化,信任所有
|
|
|
|
+ X509TrustManager x509mgr = new X509TrustManager() {
|
|
|
|
+ // 该方法检查客户端的证书,若不信任该证书则抛出异常
|
|
|
|
+ public void checkClientTrusted(X509Certificate[] xcs, String string) {
|
|
|
|
+ }
|
|
|
|
+ // 该方法检查服务端的证书,若不信任该证书则抛出异常
|
|
|
|
+ public void checkServerTrusted(X509Certificate[] xcs, String string) {
|
|
|
|
+ }
|
|
|
|
+ // 返回受信任的X509证书数组。
|
|
|
|
+ public X509Certificate[] getAcceptedIssuers() {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+ SSLContext sslContext = null;
|
|
|
|
+ try {
|
|
|
|
+ sslContext = SSLContext.getInstance("TLS");
|
|
|
|
+ sslContext.init(null, new TrustManager[] { x509mgr }, null);
|
|
|
|
+ SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new HostnameVerifier() {
|
|
|
|
+ @Override
|
|
|
|
+ public boolean verify(String hostname, SSLSession sslSession) {
|
|
|
|
+ // 对hostname不做验证信任所有,此处有安全隐患
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ return HttpClients.custom().setSSLSocketFactory(sslsf).build();
|
|
|
|
+ } catch (NoSuchAlgorithmException e) {
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
+ } catch (KeyManagementException e) {
|
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
|
+ }
|
|
|
|
+ return HttpClients.createDefault();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|