deason 4 vuotta sitten
vanhempi
commit
2746370a76

+ 308 - 311
src/main/java/cn/com/qmth/dp/examcloud/oe/modules/update_correct_answer/util/OKHttpUtil.java

@@ -1,5 +1,15 @@
 package cn.com.qmth.dp.examcloud.oe.modules.update_correct_answer.util;
 
+import cn.com.qmth.dp.examcloud.oe.modules.load_question_cache.TrustAllTrustManager;
+import cn.com.qmth.examcloud.commons.util.HttpMethod;
+import okhttp3.*;
+import okhttp3.Request.Builder;
+import org.apache.commons.collections.CollectionUtils;
+
+import javax.net.ssl.HostnameVerifier;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLSession;
+import javax.net.ssl.TrustManager;
 import java.io.IOException;
 import java.security.KeyManagementException;
 import java.security.NoSuchAlgorithmException;
@@ -8,25 +18,6 @@ import java.util.Map;
 import java.util.Map.Entry;
 import java.util.concurrent.TimeUnit;
 
-import javax.net.ssl.HostnameVerifier;
-import javax.net.ssl.SSLContext;
-import javax.net.ssl.SSLSession;
-import javax.net.ssl.TrustManager;
-
-import org.apache.commons.collections.CollectionUtils;
-
-import cn.com.qmth.dp.examcloud.oe.modules.load_question_cache.TrustAllTrustManager;
-import cn.com.qmth.examcloud.commons.util.HttpMethod;
-import okhttp3.FormBody;
-import okhttp3.MediaType;
-import okhttp3.MultipartBody;
-import okhttp3.OkHttpClient;
-import okhttp3.Request;
-import okhttp3.Request.Builder;
-import okhttp3.RequestBody;
-import okhttp3.Response;
-import okhttp3.ResponseBody;
-
 /**
  * OKHttp
  *
@@ -36,306 +27,312 @@ import okhttp3.ResponseBody;
  */
 public class OKHttpUtil {
 
-//	private static final Logger LOG = LoggerFactory.getLogger(OKHttpUtil.class);
-
-	public static final class MediaTypes {
-
-		public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
-	}
-
-	/**
-	 * 请求体构建器
-	 *
-	 * @author WANGWEI
-	 * @date 2019年4月10日
-	 * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
-	 */
-	public static interface RequestBodyBuilder {
-		RequestBody build();
-	}
-
-	/**
-	 * json请求体构建器
-	 *
-	 * @author WANGWEI
-	 * @date 2019年4月10日
-	 * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
-	 */
-	public static final class JsonBodyBuilder implements RequestBodyBuilder {
-
-		private String json;
-
-		public JsonBodyBuilder(String json) {
-			super();
-			this.json = json;
-		}
-
-		@Override
-		public RequestBody build() {
-			return RequestBody.create(MediaTypes.JSON, json);
-		}
-
-		@Override
-		public String toString() {
-			return json;
-		}
-	}
-
-	private static OkHttpClient okHttpClient;
-
-	static {
-		SSLContext sslContext = null;
-		try {
-			sslContext = SSLContext.getInstance("TLS");
-			sslContext.init(null, new TrustManager[]{new TrustAllTrustManager()}, null);
-		} catch (KeyManagementException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		} catch (NoSuchAlgorithmException e) {
-			// TODO Auto-generated catch block
-			e.printStackTrace();
-		}
-		okHttpClient = new OkHttpClient.Builder().connectTimeout(60, TimeUnit.SECONDS)
-				.readTimeout(60, TimeUnit.SECONDS).sslSocketFactory(sslContext.getSocketFactory(), new TrustAllTrustManager()).hostnameVerifier(new HostnameVerifier() {
-					
-					@Override
-					public boolean verify(String hostname, SSLSession session) {
-						return true;
-					}
-				}).build();
-	}
-
-	public static OkHttpClient getOkHttpClient() {
-		return okHttpClient;
-	}
-
-	/**
-	 * 发送请求 (带json请求体)
-	 *
-	 * @author WANGWEI
-	 * @param httpMethod
-	 * @param url
-	 * @param headers
-	 * @param jsonBody
-	 * @return
-	 */
-	public static Response call(HttpMethod httpMethod, String url, Map<String, String> headers,
-								String jsonBody) {
-		return call(httpMethod, url, headers, new JsonBodyBuilder(jsonBody));
-	}
-
-	/**
-	 * 发送请求 (带请求体)
-	 *
-	 * @author WANGWEI
-	 * @param httpMethod
-	 * @param url
-	 * @param headers
-	 * @param requestBodyBuilder
-	 * @return
-	 */
-	public static Response call(HttpMethod httpMethod, String url, Map<String, String> headers,
-			RequestBodyBuilder requestBodyBuilder) {
+    //	private static final Logger LOG = LoggerFactory.getLogger(OKHttpUtil.class);
+
+    public static final class MediaTypes {
+
+        public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
+
+    }
+
+    /**
+     * 请求体构建器
+     *
+     * @author WANGWEI
+     * @date 2019年4月10日
+     * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
+     */
+    public static interface RequestBodyBuilder {
+
+        RequestBody build();
+
+    }
+
+    /**
+     * json请求体构建器
+     *
+     * @author WANGWEI
+     * @date 2019年4月10日
+     * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
+     */
+    public static final class JsonBodyBuilder implements RequestBodyBuilder {
+
+        private String json;
+
+        public JsonBodyBuilder(String json) {
+            super();
+            this.json = json;
+        }
+
+        @Override
+        public RequestBody build() {
+            return RequestBody.create(MediaTypes.JSON, json);
+        }
+
+        @Override
+        public String toString() {
+            return json;
+        }
+
+    }
+
+    private static OkHttpClient okHttpClient;
+
+    static {
+        SSLContext sslContext = null;
+        try {
+            sslContext = SSLContext.getInstance("TLS");
+            sslContext.init(null, new TrustManager[]{new TrustAllTrustManager()}, null);
+        } catch (KeyManagementException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        } catch (NoSuchAlgorithmException e) {
+            // TODO Auto-generated catch block
+            e.printStackTrace();
+        }
+        okHttpClient = new OkHttpClient.Builder()
+                .connectTimeout(60, TimeUnit.SECONDS)
+                .readTimeout(60, TimeUnit.SECONDS)
+                .writeTimeout(60, TimeUnit.SECONDS)
+                .sslSocketFactory(sslContext.getSocketFactory(), new TrustAllTrustManager()).hostnameVerifier(new HostnameVerifier() {
+                    @Override
+                    public boolean verify(String hostname, SSLSession session) {
+                        return true;
+                    }
+                }).build();
+    }
+
+    public static OkHttpClient getOkHttpClient() {
+        return okHttpClient;
+    }
+
+    /**
+     * 发送请求 (带json请求体)
+     *
+     * @param httpMethod
+     * @param url
+     * @param headers
+     * @param jsonBody
+     * @return
+     * @author WANGWEI
+     */
+    public static Response call(HttpMethod httpMethod, String url, Map<String, String> headers,
+                                String jsonBody) {
+        return call(httpMethod, url, headers, new JsonBodyBuilder(jsonBody));
+    }
+
+    /**
+     * 发送请求 (带请求体)
+     *
+     * @param httpMethod
+     * @param url
+     * @param headers
+     * @param requestBodyBuilder
+     * @return
+     * @author WANGWEI
+     */
+    public static Response call(HttpMethod httpMethod, String url, Map<String, String> headers,
+                                RequestBodyBuilder requestBodyBuilder) {
 
 		/*LOG.info("[okhttp3] new call: " + httpMethod + " " + url);
 		LOG.info("[okhttp3] headers: " + JsonUtil.toJson(headers));
 		LOG.info("[okhttp3] body: " + requestBodyBuilder);*/
 
-		Builder builder = null;
-		if (httpMethod.equals(HttpMethod.POST)) {
-			builder = new Builder().url(url).post(requestBodyBuilder.build());
-		} else if (httpMethod.equals(HttpMethod.PUT)) {
-			builder = new Builder().url(url).put(requestBodyBuilder.build());
-		} else if (httpMethod.equals(HttpMethod.DELETE)) {
-			builder = new Builder().url(url).delete(requestBodyBuilder.build());
-		}
-
-		if (null != headers && 0 != headers.size()) {
-			for (Entry<String, String> entry : headers.entrySet()) {
-				builder.addHeader(entry.getKey(), entry.getValue());
-			}
-		}
-
-		Request request = builder.build();
-
-		Response response = null;
-		try {
-			response = okHttpClient.newCall(request).execute();
-			return response;
-		} catch (IOException e) {
-			throw new RuntimeException(e);
-		}
-	}
-
-	/**
-	 * 发送请求
-	 *
-	 * @author WANGWEI
-	 * @param httpMethod
-	 * @param url
-	 * @return
-	 */
-	public static Response call(HttpMethod httpMethod, String url) {
-		return call(httpMethod, url, null);
-	}
-
-	/**
-	 * 发送请求
-	 *
-	 * @author WANGWEI
-	 * @param httpMethod
-	 * @param url
-	 * @param headers
-	 * @return
-	 */
-	public static Response call(HttpMethod httpMethod, String url, Map<String, String> headers) {
-
-//		LOG.info("[okhttp3] new call: " + httpMethod + " " + url);
-//		LOG.info("[okhttp3] headers: " + JsonUtil.toJson(headers));
-
-		Builder builder = null;
-		if (httpMethod.equals(HttpMethod.GET)) {
-			builder = new Builder().url(url);
-		} else if (httpMethod.equals(HttpMethod.POST)) {
-			builder = new Builder().url(url).post(new FormBody.Builder().build());
-		} else if (httpMethod.equals(HttpMethod.PUT)) {
-			builder = new Builder().url(url).put(new FormBody.Builder().build());
-		} else if (httpMethod.equals(HttpMethod.DELETE)) {
-			builder = new Builder().url(url).delete(new FormBody.Builder().build());
-		}
-
-		if (null != headers && 0 != headers.size()) {
-			for (Entry<String, String> entry : headers.entrySet()) {
-				builder.addHeader(entry.getKey(), entry.getValue());
-			}
-		}
-
-		Request request = builder.build();
-
-		Response response = null;
-		try {
-			response = okHttpClient.newCall(request).execute();
-			return response;
-		} catch (IOException e) {
-			throw new RuntimeException(e);
-		}
-	}
-
-	/**
-	 * 发送请求 (表单)
-	 *
-	 * @author WANGWEI
-	 * @param httpMethod
-	 * @param url
-	 * @param headers
-	 * @param params
-	 * @return
-	 */
-	public static Response call(HttpMethod httpMethod, String url, Map<String, String> headers,
-			Map<String, String> params) {
-
-//		LOG.info("[okhttp3] new call: " + httpMethod + " " + url);
-//		LOG.info("[okhttp3] headers: " + JsonUtil.toJson(headers));
-//		LOG.info("[okhttp3] params: " + JsonUtil.toJson(params));
-
-		FormBody.Builder formBody = new FormBody.Builder();
-
-		if (null != params && 0 != params.size()) {
-			for (Entry<String, String> entry : params.entrySet()) {
-				formBody.add(entry.getKey(), entry.getValue());
-			}
-		}
-
-		Builder builder = null;
-		if (httpMethod.equals(HttpMethod.POST)) {
-			builder = new Builder().url(url).post(formBody.build());
-		} else if (httpMethod.equals(HttpMethod.PUT)) {
-			builder = new Builder().url(url).put(formBody.build());
-		} else if (httpMethod.equals(HttpMethod.DELETE)) {
-			builder = new Builder().url(url).delete(formBody.build());
-		}
-
-		if (null != headers && 0 != headers.size()) {
-			for (Entry<String, String> entry : headers.entrySet()) {
-				builder.addHeader(entry.getKey(), entry.getValue());
-			}
-		}
-
-		Request request = builder.build();
-
-		Response response = null;
-		try {
-			response = okHttpClient.newCall(request).execute();
-			return response;
-		} catch (IOException e) {
-			throw new RuntimeException(e);
-		}
-	}
-
-	/**
-	 * 发送请求 (包含文件表单)
-	 *
-	 * @author WANGWEI
-	 * @param httpMethod
-	 * @param url
-	 * @param headers
-	 * @param params
-	 * @param formFilePartList
-	 * @return
-	 */
-	public static Response call(HttpMethod httpMethod, String url, Map<String, String> headers,
-			Map<String, String> params, List<FormFilePart> formFilePartList) {
-
-//		LOG.info("[okhttp3] new call: " + httpMethod + " " + url);
-//		LOG.info("[okhttp3] headers: " + JsonUtil.toJson(headers));
-//		LOG.info("[okhttp3] params: " + JsonUtil.toJson(params));
-
-		MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder()
-				.setType(MultipartBody.ALTERNATIVE);
-
-		if (null != params) {
-			for (Entry<String, String> entry : params.entrySet()) {
-				multipartBodyBuilder.addFormDataPart(entry.getKey(), entry.getValue());
-			}
-		}
-
-		if (CollectionUtils.isNotEmpty(formFilePartList)) {
-			MediaType type = MediaType.parse("application/octet-stream");
-			for (FormFilePart part : formFilePartList) {
-				RequestBody fileBody = RequestBody.create(type, part.getFile());
-				multipartBodyBuilder.addFormDataPart(part.getParamName(), part.getFilename(),
-						fileBody);
-			}
-		}
-
-		Builder builder = new Builder().url(url).post(multipartBodyBuilder.build());
-		if (null != headers && 0 != headers.size()) {
-			for (Entry<String, String> entry : headers.entrySet()) {
-				builder.addHeader(entry.getKey(), entry.getValue());
-			}
-		}
-
-		Request request = builder.build();
-
-		Response response = null;
-		try {
-			response = okHttpClient.newCall(request).execute();
-			return response;
-		} catch (IOException e) {
-			throw new RuntimeException(e);
-		}
-	}
-	
-	public static String call(Request.Builder request) {
+        Builder builder = null;
+        if (httpMethod.equals(HttpMethod.POST)) {
+            builder = new Builder().url(url).post(requestBodyBuilder.build());
+        } else if (httpMethod.equals(HttpMethod.PUT)) {
+            builder = new Builder().url(url).put(requestBodyBuilder.build());
+        } else if (httpMethod.equals(HttpMethod.DELETE)) {
+            builder = new Builder().url(url).delete(requestBodyBuilder.build());
+        }
+
+        if (null != headers && 0 != headers.size()) {
+            for (Entry<String, String> entry : headers.entrySet()) {
+                builder.addHeader(entry.getKey(), entry.getValue());
+            }
+        }
+
+        Request request = builder.build();
+
+        Response response = null;
+        try {
+            response = okHttpClient.newCall(request).execute();
+            return response;
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * 发送请求
+     *
+     * @param httpMethod
+     * @param url
+     * @return
+     * @author WANGWEI
+     */
+    public static Response call(HttpMethod httpMethod, String url) {
+        return call(httpMethod, url, null);
+    }
+
+    /**
+     * 发送请求
+     *
+     * @param httpMethod
+     * @param url
+     * @param headers
+     * @return
+     * @author WANGWEI
+     */
+    public static Response call(HttpMethod httpMethod, String url, Map<String, String> headers) {
+
+        //		LOG.info("[okhttp3] new call: " + httpMethod + " " + url);
+        //		LOG.info("[okhttp3] headers: " + JsonUtil.toJson(headers));
+
+        Builder builder = null;
+        if (httpMethod.equals(HttpMethod.GET)) {
+            builder = new Builder().url(url);
+        } else if (httpMethod.equals(HttpMethod.POST)) {
+            builder = new Builder().url(url).post(new FormBody.Builder().build());
+        } else if (httpMethod.equals(HttpMethod.PUT)) {
+            builder = new Builder().url(url).put(new FormBody.Builder().build());
+        } else if (httpMethod.equals(HttpMethod.DELETE)) {
+            builder = new Builder().url(url).delete(new FormBody.Builder().build());
+        }
+
+        if (null != headers && 0 != headers.size()) {
+            for (Entry<String, String> entry : headers.entrySet()) {
+                builder.addHeader(entry.getKey(), entry.getValue());
+            }
+        }
+
+        Request request = builder.build();
+
+        Response response = null;
+        try {
+            response = okHttpClient.newCall(request).execute();
+            return response;
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * 发送请求 (表单)
+     *
+     * @param httpMethod
+     * @param url
+     * @param headers
+     * @param params
+     * @return
+     * @author WANGWEI
+     */
+    public static Response call(HttpMethod httpMethod, String url, Map<String, String> headers,
+                                Map<String, String> params) {
+
+        //		LOG.info("[okhttp3] new call: " + httpMethod + " " + url);
+        //		LOG.info("[okhttp3] headers: " + JsonUtil.toJson(headers));
+        //		LOG.info("[okhttp3] params: " + JsonUtil.toJson(params));
+
+        FormBody.Builder formBody = new FormBody.Builder();
+
+        if (null != params && 0 != params.size()) {
+            for (Entry<String, String> entry : params.entrySet()) {
+                formBody.add(entry.getKey(), entry.getValue());
+            }
+        }
+
+        Builder builder = null;
+        if (httpMethod.equals(HttpMethod.POST)) {
+            builder = new Builder().url(url).post(formBody.build());
+        } else if (httpMethod.equals(HttpMethod.PUT)) {
+            builder = new Builder().url(url).put(formBody.build());
+        } else if (httpMethod.equals(HttpMethod.DELETE)) {
+            builder = new Builder().url(url).delete(formBody.build());
+        }
+
+        if (null != headers && 0 != headers.size()) {
+            for (Entry<String, String> entry : headers.entrySet()) {
+                builder.addHeader(entry.getKey(), entry.getValue());
+            }
+        }
+
+        Request request = builder.build();
+
+        Response response = null;
+        try {
+            response = okHttpClient.newCall(request).execute();
+            return response;
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * 发送请求 (包含文件表单)
+     *
+     * @param httpMethod
+     * @param url
+     * @param headers
+     * @param params
+     * @param formFilePartList
+     * @return
+     * @author WANGWEI
+     */
+    public static Response call(HttpMethod httpMethod, String url, Map<String, String> headers,
+                                Map<String, String> params, List<FormFilePart> formFilePartList) {
+
+        //		LOG.info("[okhttp3] new call: " + httpMethod + " " + url);
+        //		LOG.info("[okhttp3] headers: " + JsonUtil.toJson(headers));
+        //		LOG.info("[okhttp3] params: " + JsonUtil.toJson(params));
+
+        MultipartBody.Builder multipartBodyBuilder = new MultipartBody.Builder()
+                .setType(MultipartBody.ALTERNATIVE);
+
+        if (null != params) {
+            for (Entry<String, String> entry : params.entrySet()) {
+                multipartBodyBuilder.addFormDataPart(entry.getKey(), entry.getValue());
+            }
+        }
+
+        if (CollectionUtils.isNotEmpty(formFilePartList)) {
+            MediaType type = MediaType.parse("application/octet-stream");
+            for (FormFilePart part : formFilePartList) {
+                RequestBody fileBody = RequestBody.create(type, part.getFile());
+                multipartBodyBuilder.addFormDataPart(part.getParamName(), part.getFilename(),
+                        fileBody);
+            }
+        }
+
+        Builder builder = new Builder().url(url).post(multipartBodyBuilder.build());
+        if (null != headers && 0 != headers.size()) {
+            for (Entry<String, String> entry : headers.entrySet()) {
+                builder.addHeader(entry.getKey(), entry.getValue());
+            }
+        }
+
+        Request request = builder.build();
+
+        Response response = null;
+        try {
+            response = okHttpClient.newCall(request).execute();
+            return response;
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static String call(Request.Builder request) {
         try (Response response = okHttpClient.newCall(request.build()).execute();) {
-        	String res=response.body().string();
+            String res = response.body().string();
             if (response.code() != 200) {
-    			throw new RuntimeException(res);
-    		}else {
-    			return res;
-    		}
+                throw new RuntimeException(res);
+            } else {
+                return res;
+            }
         } catch (Exception e) {
-        	throw new RuntimeException(e);
+            throw new RuntimeException(e);
         }
     }