|
@@ -0,0 +1,289 @@
|
|
|
+package cn.com.qmth.importpaper;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Map.Entry;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
+
|
|
|
+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;
|
|
|
+
|
|
|
+/**
|
|
|
+ * OKHttp
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @date 2018年9月6日
|
|
|
+ * @Copyright (c) 2018-2020 WANGWEI [QQ:522080330] All Rights Reserved.
|
|
|
+ */
|
|
|
+public class OKHttpUtil {
|
|
|
+
|
|
|
+
|
|
|
+ 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 {
|
|
|
+ okHttpClient = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS)
|
|
|
+ .readTimeout(300, TimeUnit.SECONDS).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) {
|
|
|
+
|
|
|
+
|
|
|
+ Builder builder = null;
|
|
|
+ if (httpMethod.equals(HttpMethod.POST)) {
|
|
|
+ builder = new Request.Builder().url(url).post(requestBodyBuilder.build());
|
|
|
+ } else if (httpMethod.equals(HttpMethod.PUT)) {
|
|
|
+ builder = new Request.Builder().url(url).put(requestBodyBuilder.build());
|
|
|
+ } else if (httpMethod.equals(HttpMethod.DELETE)) {
|
|
|
+ builder = new Request.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) {
|
|
|
+
|
|
|
+
|
|
|
+ Builder builder = null;
|
|
|
+ if (httpMethod.equals(HttpMethod.GET)) {
|
|
|
+ builder = new Request.Builder().url(url);
|
|
|
+ } else if (httpMethod.equals(HttpMethod.POST)) {
|
|
|
+ builder = new Request.Builder().url(url).post(new FormBody.Builder().build());
|
|
|
+ } else if (httpMethod.equals(HttpMethod.PUT)) {
|
|
|
+ builder = new Request.Builder().url(url).put(new FormBody.Builder().build());
|
|
|
+ } else if (httpMethod.equals(HttpMethod.DELETE)) {
|
|
|
+ builder = new Request.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) {
|
|
|
+
|
|
|
+ okhttp3.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 Request.Builder().url(url).post(formBody.build());
|
|
|
+ } else if (httpMethod.equals(HttpMethod.PUT)) {
|
|
|
+ builder = new Request.Builder().url(url).put(formBody.build());
|
|
|
+ } else if (httpMethod.equals(HttpMethod.DELETE)) {
|
|
|
+ builder = new Request.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) {
|
|
|
+
|
|
|
+
|
|
|
+ okhttp3.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 Request.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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|