123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- package cn.com.qmth.examcloud.commons.util;
- 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.collections.CollectionUtils;
- import cn.com.qmth.examcloud.commons.helpers.FormFilePart;
- import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
- import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
- 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 http://www.qmth.com.cn/ All Rights Reserved.
- */
- public class OKHttpUtil {
- private static final ExamCloudLog LOG = ExamCloudLogFactory.getLog(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 http://www.qmth.com.cn/ All Rights Reserved.
- */
- public static interface RequestBodyBuilder {
- RequestBody build();
- }
- /**
- * json请求体构建器
- *
- * @author WANGWEI
- * @date 2019年4月10日
- * @Copyright (c) 2018-2020 http://www.qmth.com.cn/ 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(20, 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) {
- 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.GET)) {
- builder = new Request.Builder().url(url).get();
- } else 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) {
- LOG.info("[okhttp3] new call: " + httpMethod + " " + url);
- LOG.info("[okhttp3] headers: " + JsonUtil.toJson(headers));
- Builder builder = null;
- if (httpMethod.equals(HttpMethod.GET)) {
- builder = new Request.Builder().url(url).get();
- } 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) {
- LOG.info("[okhttp3] new call: " + httpMethod + " " + url);
- LOG.info("[okhttp3] headers: " + JsonUtil.toJson(headers));
- LOG.info("[okhttp3] params: " + JsonUtil.toJson(params));
- Builder builder = null;
- if (httpMethod.equals(HttpMethod.GET)) {
- url = UrlUtil.joinParams(url, params);
- builder = new Request.Builder().url(url).get();
- } else {
- 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());
- }
- }
- 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) {
- LOG.info("[okhttp3] new call: " + httpMethod + " " + url);
- LOG.info("[okhttp3] headers: " + JsonUtil.toJson(headers));
- LOG.info("[okhttp3] params: " + JsonUtil.toJson(params));
- 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);
- }
- }
- }
|