| package cc.mrbird.febs.mall.chain.trcCoin; | 
|   | 
| import okhttp3.*; | 
| import org.slf4j.Logger; | 
| import org.slf4j.LoggerFactory; | 
|   | 
| import java.io.IOException; | 
| import java.nio.charset.StandardCharsets; | 
| import java.util.Map; | 
| import java.util.Objects; | 
|   | 
| public class OkHttpUtil2 { | 
|     protected final static Logger logger = LoggerFactory.getLogger(OkHttpUtil2.class); | 
|     private static Dispatcher dispatcher = new Dispatcher(); | 
|   | 
|     static { | 
|         dispatcher.setMaxRequests(200); | 
|         dispatcher.setMaxRequestsPerHost(100); | 
|     } | 
|   | 
|     private static OkHttpClient httpClient = new OkHttpClient.Builder().dispatcher(dispatcher).build(); | 
|   | 
|     public static byte[] doGet(String url, Map<String, String[]> header, Map<String, String[]> params, String expectContentType) { | 
|         Request.Builder builder = new Request.Builder(); | 
|         addHeader(builder, header); | 
|         addUrlParam(builder, url, params); | 
|         return requestExec(builder.build(), expectContentType); | 
|     } | 
|   | 
|     public static byte[] doGetSingle(String url, Map<String, String> header, Map<String, String> params, String expectContentType) { | 
|         Request.Builder builder = new Request.Builder(); | 
|         addHeaderSingle(builder, header); | 
|         addUrlParamSingle(builder, url, params); | 
|         return requestExec(builder.build(), expectContentType); | 
|     } | 
|   | 
|     public static byte[] doPost(String url, Map<String, String[]> header, Map<String, String[]> body, String expectContentType) { | 
|         Request.Builder builder = new Request.Builder().url(url); | 
|         addHeader(builder, header); | 
|         addBodyParam(builder, body, "POST"); | 
|         return requestExec(builder.build(), expectContentType); | 
|     } | 
|   | 
|   | 
|   | 
|     private static void addHeaderSingle(Request.Builder builder, Map<String, String> header) { | 
|         if (header == null) { | 
|             return; | 
|         } | 
|         for (String key : header.keySet()) { | 
|             String value = header.get(key); | 
|             if (value != null) { | 
|                 builder.addHeader(key, value); | 
|             } | 
|         } | 
|     } | 
|   | 
|     private static void addHeader(Request.Builder builder, Map<String, String[]> header) { | 
|         if (header == null) { | 
|             return; | 
|         } | 
|         for (String key : header.keySet()) { | 
|             String[] values = header.get(key); | 
|             if (values != null) { | 
|                 for (String value : values) { | 
|                     builder.addHeader(key, value); | 
|                 } | 
|             } | 
|         } | 
|     } | 
|   | 
|     private static void addUrlParam(Request.Builder builder, String url, Map<String, String[]> params) { | 
|         if (params == null) { | 
|             return; | 
|         } | 
|         HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder(); | 
|         for (String key : params.keySet()) { | 
|             String[] values = params.get(key); | 
|             if (values != null) { | 
|                 for (String value : values) { | 
|                     urlBuilder.addQueryParameter(key, value); | 
|                 } | 
|             } | 
|         } | 
|         builder.url(urlBuilder.build()); | 
|     } | 
|   | 
|     private static void addUrlParamSingle(Request.Builder builder, String url, Map<String, String> params) { | 
|         if (params == null) { | 
|             return; | 
|         } | 
|         HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder(); | 
|         for (String key : params.keySet()) { | 
|             String value = params.get(key); | 
|             if (value != null) { | 
|                 urlBuilder.addQueryParameter(key, value); | 
|             } | 
|         } | 
|         builder.url(urlBuilder.build()); | 
|     } | 
|   | 
|     private static void addBodyParam(Request.Builder builder, Map<String, String[]> body, String method) { | 
|         if (body == null) { | 
|             return; | 
|         } | 
|         FormBody.Builder formBodyBuilder = new FormBody.Builder(StandardCharsets.UTF_8); | 
|         for (String key : body.keySet()) { | 
|             String[] values = body.get(key); | 
|             if (values != null) { | 
|                 for (String value : values) { | 
|                     formBodyBuilder.add(key, value); | 
|                 } | 
|             } | 
|         } | 
|         builder.method(method, formBodyBuilder.build()); | 
|     } | 
|   | 
|     private static byte[] requestExec(Request request, String expectContentType) { | 
|         Objects.requireNonNull(request, "okHttp request is null"); | 
|   | 
|         try (Response response = httpClient.newCall(request).execute()) { | 
|             if (response.code() == 200) { | 
|                 ResponseBody body = response.body(); | 
|                 if (body != null) { | 
|                     byte[] bytes = body.bytes(); | 
|                     String contentType = response.header("Content-Type"); | 
|                     if (contentType != null && !contentType.contains(expectContentType)) { | 
|                         String res = new String(bytes, StandardCharsets.UTF_8); | 
|                         System.out.println(res); | 
|                         return bytes; | 
|                     } | 
|                     return bytes; | 
|                 } | 
|                 logger.error("response body is null"); | 
|                 System.out.println("response body is null"); | 
|             } else { | 
|                 ResponseBody body = response.body(); | 
|                 String res = ""; | 
|                 byte[] bytes = null; | 
|                 if (body != null) { | 
|                     bytes = body.bytes(); | 
|                     String contentType = response.header("Content-Type"); | 
|                     if (contentType != null && !contentType.contains(expectContentType)) { | 
|                         //res = new String(body.bytes(), StandardCharsets.UTF_8); | 
|                         //return body.bytes(); | 
|                     } | 
|                     res = new String(bytes, StandardCharsets.UTF_8); | 
|   | 
|                 } | 
|                 logger.error("request failed, http code:{},responseBody:{} ", response.code(), res); | 
|                 System.out.println("request failed, http code: " + response.code()); | 
|   | 
|                 return bytes; | 
|             } | 
|         } catch (IOException ioException) { | 
|             logger.error("request exec error:", ioException); | 
|             System.out.println("request exec error: " + ioException.getMessage()); | 
|         } | 
|         return null; | 
|     } | 
| } |