|
|
@@ -1,10 +1,10 @@
|
|
|
package com.middle.platform.common.utils;
|
|
|
|
|
|
-import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import okhttp3.*;
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
import javax.net.ssl.SSLSocketFactory;
|
|
|
@@ -12,10 +12,12 @@ import javax.net.ssl.TrustManager;
|
|
|
import javax.net.ssl.X509TrustManager;
|
|
|
import java.io.IOException;
|
|
|
import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.security.SecureRandom;
|
|
|
import java.security.cert.X509Certificate;
|
|
|
import java.util.LinkedHashMap;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
import java.util.concurrent.Semaphore;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@@ -28,13 +30,15 @@ public class OkHttpUtils {
|
|
|
|
|
|
private static volatile OkHttpClient okHttpClient = null;
|
|
|
private static volatile Semaphore semaphore = null;
|
|
|
- private Map<String, String> headerMap;
|
|
|
- private Map<String, String> paraMap;
|
|
|
- private Map<String, String> bodyParaMap;
|
|
|
- //json字符串
|
|
|
- private String bodyParaString;
|
|
|
+ //请求地址
|
|
|
private String url;
|
|
|
private Request.Builder request;
|
|
|
+ //Headers
|
|
|
+ private Map<String, String> headerMap;
|
|
|
+ //Params
|
|
|
+ private Map<String, String> paraMap;
|
|
|
+ // form-data(不含file)、x-www-form-urlencoded
|
|
|
+ private Map<String, String> bodyMap;
|
|
|
|
|
|
/**
|
|
|
* 初始化okHttpClient,并且允许https访问
|
|
|
@@ -44,14 +48,7 @@ public class OkHttpUtils {
|
|
|
synchronized (OkHttpUtils.class) {
|
|
|
if (okHttpClient == null) {
|
|
|
TrustManager[] trustManagers = buildTrustManagers();
|
|
|
- okHttpClient = new OkHttpClient.Builder()
|
|
|
- .connectTimeout(15, TimeUnit.SECONDS)
|
|
|
- .writeTimeout(20, TimeUnit.SECONDS)
|
|
|
- .readTimeout(20, TimeUnit.SECONDS)
|
|
|
- .sslSocketFactory(createSSLSocketFactory(trustManagers), (X509TrustManager) trustManagers[0])
|
|
|
- .hostnameVerifier((hostName, session) -> true)
|
|
|
- .retryOnConnectionFailure(true)
|
|
|
- .build();
|
|
|
+ okHttpClient = new OkHttpClient.Builder().connectTimeout(15, TimeUnit.SECONDS).writeTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS).sslSocketFactory(createSSLSocketFactory(trustManagers), (X509TrustManager) trustManagers[0]).hostnameVerifier((hostName, session) -> true).retryOnConnectionFailure(true).build();
|
|
|
addHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
|
|
|
}
|
|
|
}
|
|
|
@@ -81,11 +78,12 @@ public class OkHttpUtils {
|
|
|
public static OkHttpUtils builder() {
|
|
|
return new OkHttpUtils();
|
|
|
}
|
|
|
+ /*设置参数*/
|
|
|
|
|
|
/**
|
|
|
- * 添加url
|
|
|
+ * 添加请求url
|
|
|
*
|
|
|
- * @param url
|
|
|
+ * @param url 请求地址
|
|
|
* @return
|
|
|
*/
|
|
|
public OkHttpUtils url(String url) {
|
|
|
@@ -94,17 +92,17 @@ public class OkHttpUtils {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 添加参数
|
|
|
+ * 添加请求头
|
|
|
*
|
|
|
* @param key 参数名
|
|
|
* @param value 参数值
|
|
|
* @return
|
|
|
*/
|
|
|
- public OkHttpUtils addPara(String key, String value) {
|
|
|
- if (paraMap == null) {
|
|
|
- paraMap = new LinkedHashMap<>(16);
|
|
|
+ public OkHttpUtils addHeader(String key, String value) {
|
|
|
+ if (Objects.isNull(headerMap)) {
|
|
|
+ headerMap = new LinkedHashMap<>(16);
|
|
|
}
|
|
|
- paraMap.put(key, value);
|
|
|
+ headerMap.put(key, value);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
@@ -115,109 +113,227 @@ public class OkHttpUtils {
|
|
|
* @param value 参数值
|
|
|
* @return
|
|
|
*/
|
|
|
- public OkHttpUtils addBodyPara(String key, String value) {
|
|
|
- if (bodyParaMap == null) {
|
|
|
- bodyParaMap = new LinkedHashMap<>(16);
|
|
|
+ public OkHttpUtils addPara(String key, String value) {
|
|
|
+ if (paraMap == null) {
|
|
|
+ paraMap = new LinkedHashMap<>(16);
|
|
|
}
|
|
|
- bodyParaMap.put(key, value);
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
- public OkHttpUtils addBodyJsonStr(String string) {
|
|
|
- bodyParaString = string;
|
|
|
+ paraMap.put(key, value);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 添加请求头
|
|
|
+ * 添加body数据
|
|
|
*
|
|
|
* @param key 参数名
|
|
|
* @param value 参数值
|
|
|
* @return
|
|
|
*/
|
|
|
- public OkHttpUtils addHeader(String key, String value) {
|
|
|
- if (headerMap == null) {
|
|
|
- headerMap = new LinkedHashMap<>(16);
|
|
|
+ public OkHttpUtils addBody(String key, String value) {
|
|
|
+ if (bodyMap == null) {
|
|
|
+ bodyMap = new LinkedHashMap<>(16);
|
|
|
}
|
|
|
- headerMap.put(key, value);
|
|
|
+ bodyMap.put(key, value);
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ /*确认请求方法类型*/
|
|
|
+
|
|
|
/**
|
|
|
* 初始化get方法
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
public OkHttpUtils get() {
|
|
|
- request = new Request.Builder().get();
|
|
|
+ request = HttpFactory.requestBuilder().get();
|
|
|
request.url(buildUrl());
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 构建表单数据,不含文件
|
|
|
+ *
|
|
|
+ * @see OkHttpUtils#addBody(String, String) 参数添加
|
|
|
+ */
|
|
|
+ public OkHttpUtils postFormData() {
|
|
|
+ Request.Builder req = HttpFactory.requestBuilder();
|
|
|
+ if (!CollectionUtils.isEmpty(bodyMap)) {
|
|
|
+ req.post(HttpFactory.formBody(bodyMap));
|
|
|
+ }
|
|
|
+ req.url(buildUrl());
|
|
|
+ request = req;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 表单数据,包含文件
|
|
|
+ *
|
|
|
+ * @param key 文件key
|
|
|
+ * @param fileName 文件名
|
|
|
+ * @param file 文件字节数据
|
|
|
+ * @see OkHttpUtils#addBody(String, String) 参数添加
|
|
|
+ */
|
|
|
+ public OkHttpUtils postFormData(String key, String fileName, byte[] file) {
|
|
|
+ Request.Builder req = HttpFactory.requestBuilder();
|
|
|
+ MultipartBody.Builder formBody = HttpFactory.multipartBody();
|
|
|
+ //添加文件部分
|
|
|
+ formBody.addPart(addMultipartFile(key, fileName, file));
|
|
|
+ //添加bodyMap参数,如果不为空
|
|
|
+ if (!CollectionUtils.isEmpty(bodyMap)) {
|
|
|
+ bodyMap.forEach(formBody::addFormDataPart);
|
|
|
+ }
|
|
|
+ //设置请求体
|
|
|
+ req.post(formBody.build());
|
|
|
+ req.url(buildUrl());
|
|
|
+ request = req;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 表单数据,包含文件
|
|
|
+ *
|
|
|
+ * @param key 文件key
|
|
|
+ * @param file 文件
|
|
|
+ * @see OkHttpUtils#addBody(String, String) 参数添加
|
|
|
+ */
|
|
|
+ public OkHttpUtils postFormData(String key, MultipartFile file) throws IOException {
|
|
|
+ Request.Builder req = HttpFactory.requestBuilder();
|
|
|
+ MultipartBody.Builder formBody = HttpFactory.multipartBody();
|
|
|
+ //添加文件部分
|
|
|
+ formBody.addPart(addMultipartFile(key, file.getOriginalFilename(), file.getBytes()));
|
|
|
+ //添加bodyMap参数,如果不为空
|
|
|
+ if (!CollectionUtils.isEmpty(bodyMap)) {
|
|
|
+ bodyMap.forEach(formBody::addFormDataPart);
|
|
|
+ }
|
|
|
+ //设置请求体
|
|
|
+ req.post(formBody.build());
|
|
|
+ req.url(buildUrl());
|
|
|
+ request = req;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * post application/x-www-form-urlencoded
|
|
|
+ *
|
|
|
+ * @see OkHttpUtils#addBody(String, String) 参数添加
|
|
|
+ */
|
|
|
+ public OkHttpUtils postFormUrlencoded() {
|
|
|
+ Request.Builder req = HttpFactory.requestBuilder();
|
|
|
+ if (!CollectionUtils.isEmpty(bodyMap)) {
|
|
|
+ req.post(HttpFactory.formEncodedBody(bodyMap));
|
|
|
+ }
|
|
|
+ req.url(buildUrl());
|
|
|
+ request = req;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * post请求
|
|
|
+ *
|
|
|
+ * @see OkHttpUtils#addBody(String, String) 参数添加
|
|
|
+ */
|
|
|
+ public OkHttpUtils postRawJson() {
|
|
|
+ Request.Builder req = HttpFactory.requestBuilder();
|
|
|
+ if (!CollectionUtils.isEmpty(bodyMap)) {
|
|
|
+ req.post(RequestBody.create(bodyMap.toString(), MediaType.parse("application/json; charset=utf-8")));
|
|
|
+ }
|
|
|
+ req.url(buildUrl());
|
|
|
+ request = req;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * post请求
|
|
|
+ *
|
|
|
+ * @param json json请求参数
|
|
|
+ * @see OkHttpUtils#addBody(String, String) 参数添加
|
|
|
+ */
|
|
|
+ public OkHttpUtils postRawJson(JSON json) {
|
|
|
+ RequestBody requestBody = RequestBody.create(json.toJSONString(), MediaType.parse("application/json; charset=utf-8"));
|
|
|
+ request = HttpFactory.requestBuilder().post(requestBody).url(buildUrl());
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * post请求
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public OkHttpUtils postRawText(String string) {
|
|
|
+ Request.Builder req = HttpFactory.requestBuilder();
|
|
|
+ req.post(RequestBody.create(string, MediaType.parse("text/plain")));
|
|
|
+ req.url(buildUrl());
|
|
|
+ request = req;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+ public OkHttpUtils putRawText(String string) {
|
|
|
+ Request.Builder req = HttpFactory.requestBuilder();
|
|
|
+ req.put(RequestBody.create(string, MediaType.parse("text/plain")));
|
|
|
+ req.url(buildUrl());
|
|
|
+ request = req;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 初始化post方法,默认为JSON
|
|
|
+ *
|
|
|
+ * @return OkHttpUtils
|
|
|
+ */
|
|
|
+ public OkHttpUtils post() {
|
|
|
+ return post(PostType.RAW_JSON);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 初始化post方法
|
|
|
*
|
|
|
- * @param isJsonPost true等于json的方式提交数据,类似postman里post方法的raw
|
|
|
- * false等于普通的表单提交
|
|
|
- * @return
|
|
|
+ * @return OkHttpUtils
|
|
|
*/
|
|
|
- public OkHttpUtils post(boolean isJsonPost) {
|
|
|
- RequestBody requestBody;
|
|
|
- if (isJsonPost) {
|
|
|
- String json;
|
|
|
- if (StringUtils.isNotBlank(bodyParaString)) {
|
|
|
- json = bodyParaString;
|
|
|
- } else if (!CollectionUtils.isEmpty(bodyParaMap)) {
|
|
|
- json = JSONObject.toJSONString(bodyParaMap);
|
|
|
- } else {
|
|
|
- json = "{}";
|
|
|
+ public OkHttpUtils post(PostType postType) {
|
|
|
+ switch (postType) {
|
|
|
+ case RAW_JSON -> {
|
|
|
+ return postRawJson();
|
|
|
+ }
|
|
|
+ case FORM_DATA -> {
|
|
|
+ return postFormData();
|
|
|
}
|
|
|
- requestBody = RequestBody.create(json, MediaType.parse("application/json; charset=utf-8"));
|
|
|
- } else {
|
|
|
- FormBody.Builder formBody = new FormBody.Builder();
|
|
|
- if (bodyParaMap != null) {
|
|
|
- bodyParaMap.forEach(formBody::add);
|
|
|
+ case X_WWW_FORM_URLENCODED -> {
|
|
|
+ return postFormUrlencoded();
|
|
|
}
|
|
|
- requestBody = formBody.build();
|
|
|
+ default -> request = HttpFactory.requestBuilder().url(buildUrl());
|
|
|
}
|
|
|
- // params参数
|
|
|
- request = new Request.Builder().post(requestBody).url(buildUrl());
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
- private String buildUrl() {
|
|
|
- StringBuilder urlBuilder = new StringBuilder(url);
|
|
|
- if (paraMap != null) {
|
|
|
- urlBuilder.append("?");
|
|
|
- try {
|
|
|
- for (Map.Entry<String, String> entry : paraMap.entrySet()) {
|
|
|
- urlBuilder.append(URLEncoder.encode(entry.getKey(), "UTF-8")).
|
|
|
- append("=").
|
|
|
- append(URLEncoder.encode(entry.getValue(), "UTF-8")).
|
|
|
- append("&");
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
+ /*发起请求*/
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同步请求,返回字节数据
|
|
|
+ *
|
|
|
+ * @return 字节数据
|
|
|
+ */
|
|
|
+ public byte[] byteSync() {
|
|
|
+ setHeader(request);
|
|
|
+ try (Response response = okHttpClient.newCall(request.build()).execute()) {
|
|
|
+ if (response.isSuccessful()) {
|
|
|
+ assert response.body() != null;
|
|
|
+ return response.body().bytes();
|
|
|
}
|
|
|
- urlBuilder.deleteCharAt(urlBuilder.length() - 1);
|
|
|
+ return new byte[0];
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return new byte[0];
|
|
|
}
|
|
|
- return urlBuilder.toString();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
* 同步请求
|
|
|
*
|
|
|
- * @return
|
|
|
+ * @return 字符串数据
|
|
|
*/
|
|
|
public String sync() {
|
|
|
setHeader(request);
|
|
|
- try {
|
|
|
- Response response = okHttpClient.newCall(request.build()).execute();
|
|
|
+ try (Response response = okHttpClient.newCall(request.build()).execute()) {
|
|
|
assert response.body() != null;
|
|
|
- return response.body().string();
|
|
|
+ String string = response.body().string();
|
|
|
+ log.debug("Request Result:{}", string);
|
|
|
+ return string;
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
return "请求失败:" + e.getMessage();
|
|
|
@@ -226,9 +342,11 @@ public class OkHttpUtils {
|
|
|
|
|
|
/**
|
|
|
* 异步请求,有返回值
|
|
|
+ *
|
|
|
+ * @return 字符串数据
|
|
|
*/
|
|
|
public String async() {
|
|
|
- StringBuilder buffer = new StringBuilder("");
|
|
|
+ StringBuilder buffer = new StringBuilder();
|
|
|
setHeader(request);
|
|
|
okHttpClient.newCall(request.build()).enqueue(new Callback() {
|
|
|
@Override
|
|
|
@@ -254,7 +372,7 @@ public class OkHttpUtils {
|
|
|
/**
|
|
|
* 异步请求,带有接口回调
|
|
|
*
|
|
|
- * @param callBack
|
|
|
+ * @param callBack 回调函数
|
|
|
*/
|
|
|
public void async(ICallBack callBack) {
|
|
|
setHeader(request);
|
|
|
@@ -272,13 +390,27 @@ public class OkHttpUtils {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+ /*内部方法*/
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加表单文件参数
|
|
|
+ *
|
|
|
+ * @param key 表单对应的表
|
|
|
+ * @param fileName 文件名称
|
|
|
+ * @param file 文件字节数据
|
|
|
+ * @return MultipartBody.Part
|
|
|
+ */
|
|
|
+ private MultipartBody.Part addMultipartFile(String key, String fileName, byte[] file) {
|
|
|
+ return MultipartBody.Part.createFormData(key, fileName, RequestBody.create(file));
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 为request添加请求头
|
|
|
*
|
|
|
- * @param request
|
|
|
+ * @param request Request.Builder
|
|
|
*/
|
|
|
private void setHeader(Request.Builder request) {
|
|
|
- if (headerMap != null) {
|
|
|
+ if (!CollectionUtils.isEmpty(headerMap)) {
|
|
|
try {
|
|
|
for (Map.Entry<String, String> entry : headerMap.entrySet()) {
|
|
|
request.addHeader(entry.getKey(), entry.getValue());
|
|
|
@@ -289,11 +421,32 @@ public class OkHttpUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 请求地址,带params
|
|
|
+ *
|
|
|
+ * @return 完整的请求地址
|
|
|
+ */
|
|
|
+ private String buildUrl() {
|
|
|
+ StringBuilder urlBuilder = new StringBuilder(url);
|
|
|
+ if (paraMap != null) {
|
|
|
+ urlBuilder.append("?");
|
|
|
+ try {
|
|
|
+ for (Map.Entry<String, String> entry : paraMap.entrySet()) {
|
|
|
+ urlBuilder.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8)).append("=").append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8)).append("&");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ urlBuilder.deleteCharAt(urlBuilder.length() - 1);
|
|
|
+ }
|
|
|
+ log.debug("Request Url -> {}", urlBuilder);
|
|
|
+ return urlBuilder.toString();
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 生成安全套接字工厂,用于https请求的证书跳过
|
|
|
*
|
|
|
- * @return
|
|
|
+ * @return SSLSocketFactory
|
|
|
*/
|
|
|
private static SSLSocketFactory createSSLSocketFactory(TrustManager[] trustAllCerts) {
|
|
|
SSLSocketFactory ssfFactory = null;
|
|
|
@@ -308,22 +461,20 @@ public class OkHttpUtils {
|
|
|
}
|
|
|
|
|
|
private static TrustManager[] buildTrustManagers() {
|
|
|
- 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[]{};
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
+ 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[]{};
|
|
|
+ }
|
|
|
+ }};
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -337,6 +488,75 @@ public class OkHttpUtils {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 工厂类
|
|
|
+ */
|
|
|
+ public static class HttpFactory {
|
|
|
+ public static Request.Builder requestBuilder() {
|
|
|
+ return new Request.Builder();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static FormBody.Builder formBodyBuilder() {
|
|
|
+ return new FormBody.Builder();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static MultipartBody.Builder multipartBodyBuilder() {
|
|
|
+ return new MultipartBody.Builder();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回 multipart/form-data 类型的请求体
|
|
|
+ *
|
|
|
+ * @param map 请求参数
|
|
|
+ * @return RequestBody
|
|
|
+ */
|
|
|
+ public static RequestBody formBody(Map<String, String> map) {
|
|
|
+ FormBody.Builder formBody = formBodyBuilder();
|
|
|
+ map.forEach(formBody::add);
|
|
|
+ return formBody.build();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回 application/x-www-form-urlencoded 类型的请求体
|
|
|
+ *
|
|
|
+ * @param map 请求参数
|
|
|
+ * @return RequestBody
|
|
|
+ */
|
|
|
+ public static RequestBody formEncodedBody(Map<String, String> map) {
|
|
|
+ FormBody.Builder formBody = formBodyBuilder();
|
|
|
+ map.forEach(formBody::addEncoded);
|
|
|
+ return formBody.build();
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * 默认返回表单form-data类型的MultipartBody
|
|
|
+ *
|
|
|
+ * @return multipart/form-data MultipartBody.Builder
|
|
|
+ */
|
|
|
+ public static MultipartBody.Builder multipartBody() {
|
|
|
+ MultipartBody.Builder builder = multipartBodyBuilder();
|
|
|
+ builder.setType(MultipartBody.FORM);
|
|
|
+ return builder;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public enum PostType {
|
|
|
+ /**
|
|
|
+ * multipart/form-data
|
|
|
+ */
|
|
|
+ FORM_DATA,
|
|
|
+ /**
|
|
|
+ * application/x-www-form-urlencoded
|
|
|
+ */
|
|
|
+ X_WWW_FORM_URLENCODED,
|
|
|
+ /**
|
|
|
+ * application/json
|
|
|
+ */
|
|
|
+ RAW_JSON,
|
|
|
+ /**
|
|
|
+ * text/plain
|
|
|
+ */
|
|
|
+ RAW_TEXT
|
|
|
+ }
|
|
|
|
|
|
}
|