刘韬 2 éve
szülő
commit
87d62db53f

+ 13 - 4
DataFusion/src/com/zskk/control/ViewController.java

@@ -17,6 +17,8 @@ import com.jfinal.plugin.activerecord.Db;
 import com.jfinal.plugin.activerecord.Record;
 import com.jfinal.plugin.activerecord.Record;
 import com.jfinal.plugin.activerecord.dialect.OracleDialect;
 import com.jfinal.plugin.activerecord.dialect.OracleDialect;
 import com.jfinal.plugin.druid.DruidPlugin;
 import com.jfinal.plugin.druid.DruidPlugin;
+import com.zskk.tools.SSLSocketClient;
+
 import okhttp3.FormBody;
 import okhttp3.FormBody;
 import okhttp3.MediaType;
 import okhttp3.MediaType;
 import okhttp3.OkHttpClient;
 import okhttp3.OkHttpClient;
@@ -65,9 +67,16 @@ public class ViewController extends Controller {
 
 
 	}
 	}
 public static String postWithParameters(String url, Map<String, String> map) {
 public static String postWithParameters(String url, Map<String, String> map) {
-    	
-    	FormBody.Builder formbody = new FormBody.Builder();
-    	
+
+
+	OkHttpClient client = new OkHttpClient.Builder()
+			.sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), SSLSocketClient.getX509TrustManager())
+			.hostnameVerifier(SSLSocketClient.getHostnameVerifier())
+			.build();
+
+	   
+	FormBody.Builder formbody = new FormBody.Builder();
+
     	for (Map.Entry<String, String> entry : map.entrySet()) {
     	for (Map.Entry<String, String> entry : map.entrySet()) {
     		formbody.add(entry.getKey(), entry.getValue());
     		formbody.add(entry.getKey(), entry.getValue());
 		}
 		}
@@ -78,7 +87,7 @@ public static String postWithParameters(String url, Map<String, String> map) {
 		        .post(requestBody)
 		        .post(requestBody)
 		        .build();
 		        .build();
 
 
-		    try (Response response = OKHTTP_CLIENT.newCall(request).execute()) {
+		    try (Response response = client.newCall(request).execute()) {
 		      if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
 		      if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
 		      String content = response.body().string();
 		      String content = response.body().string();
 		      return content;
 		      return content;

+ 64 - 0
DataFusion/src/com/zskk/tools/SSLSocketClient.java

@@ -0,0 +1,64 @@
+package com.zskk.tools;
+
+import javax.net.ssl.*;
+import java.security.KeyStore;
+import java.security.SecureRandom;
+import java.security.cert.X509Certificate;
+import java.util.Arrays;
+
+
+public class SSLSocketClient {
+
+    //获取这个SSLSocketFactory
+    public static SSLSocketFactory getSSLSocketFactory() {
+        try {
+            SSLContext sslContext = SSLContext.getInstance("SSL");
+            sslContext.init(null, getTrustManager(), new SecureRandom());
+            return sslContext.getSocketFactory();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    //获取TrustManager
+    private static TrustManager[] getTrustManager() {
+        return new TrustManager[]{
+                new X509TrustManager() {
+                    @Override
+                    public void checkClientTrusted(X509Certificate[] chain, String authType) {
+                    }
+
+                    @Override
+                    public void checkServerTrusted(X509Certificate[] chain, String authType) {
+                    }
+
+                    @Override
+                    public X509Certificate[] getAcceptedIssuers() {
+                        return new X509Certificate[]{};
+                    }
+                }
+        };
+    }
+
+    //获取HostnameVerifier
+    public static HostnameVerifier getHostnameVerifier() {
+        return (s, sslSession) -> true;
+    }
+
+    public static X509TrustManager getX509TrustManager() {
+        X509TrustManager trustManager = null;
+        try {
+            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
+            trustManagerFactory.init((KeyStore) null);
+            TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
+            if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
+                throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
+            }
+            trustManager = (X509TrustManager) trustManagers[0];
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        return trustManager;
+    }
+}