1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
| @Slf4j @Component public class OKHttpUtil {
private static final OkHttpClient client = new OkHttpClient();
public static String get(String url, Map<String, String> headers, Map<String, Object> params) throws Exception { return dealRequestByMethod(HttpMethod.GET, url, headers, params,false); }
public static byte[] getByte(String url, Map<String, String> headers, Map<String, Object> params) throws Exception { Call call=getCall(HttpMethod.GET, url, headers, params, false); try (Response response = call.execute()) { return response.body().bytes(); } catch (Exception e) { log.error("远程接口请求失败![url:{}][headers:{}][params:{}][reason:{}]", url, JSON.toJSONString(headers), JSON.toJSONString(params), e); throw new Exception("远程服务暂时不可用, 请稍后重试!", e); } }
public static <T> T getEntity( String url, Map<String, String> headers, Map<String, Object> params, Class<T> clazz) throws Exception { String resultStr = dealRequestByMethod(HttpMethod.GET, url, headers, params,false); T obj = JSON.parseObject(resultStr, clazz); return obj; }
public static <T> T postEntity( String url, Map<String, String> headers, Map<String, Object> params, Class<T> clazz) throws Exception { String resultStr = dealRequestByMethod(HttpMethod.POST, url, headers, params,false); T obj = JSON.parseObject(resultStr, clazz); return obj; }
public static String post(String url, Map<String, String> headers, Map<String, Object> params) throws Exception { return dealRequestByMethod(HttpMethod.POST, url, headers, params,false); }
public static <T> T postWithFormUrlencodedEntity( String url, Map<String, String> headers, Map<String, Object> params, Class<T> clazz) throws Exception { String resultStr = dealRequestByMethod(HttpMethod.POST, url, headers, params, true); T obj = JSON.parseObject(resultStr, clazz); return obj; }
private static String dealRequestByMethod(HttpMethod httpMethod, String url, Map<String, String> headers, Map<String, Object> params, Boolean isFormUrlencode) throws Exception { Call call=getCall(httpMethod, url, headers, params, isFormUrlencode);
try (Response response = call.execute()) { return response.body().string(); } catch (Exception e) { log.error("远程接口请求失败![url:{}][headers:{}][params:{}][reason:{}]", url, JSON.toJSONString(headers), JSON.toJSONString(params), e); throw new Exception("远程服务暂时不可用, 请稍后重试!", e); } }
private static void dealRequestByMethodWithCallback(HttpMethod httpMethod, String url, Map<String, String> headers, Map<String, Object> params, Boolean isFormUrlencode,Callback callback) throws Exception { Call call=getCall(httpMethod, url, headers, params, isFormUrlencode); syncReturnResult(url, headers, params, call,callback); }
private static Call getCall(HttpMethod httpMethod, String url, Map<String, String> headers, Map<String, Object> params, Boolean isFormUrlencode) throws Exception { Request.Builder requestBuilder = new Request.Builder(); addHeaders(headers, requestBuilder); switch (httpMethod) { case GET: default: addGetParams(url, params, requestBuilder); break; case POST: addPostParams(url, params, requestBuilder, isFormUrlencode); break; } Request request = requestBuilder.build(); Call call = client.newCall(request);
return call; }
public static final MediaType JSON_CHARSET = MediaType.get("application/json; charset=utf-8");
private static void syncReturnResult(String url, Map<String, String> headers, Map<String, Object> params, Call call,Callback callback) throws Exception {
try { call.enqueue(callback); } catch (Exception e) { log.error("远程接口请求失败![url:{}][headers:{}][params:{}][reason:{}]", url, JSON.toJSONString(headers), JSON.toJSONString(params), e); throw new Exception("远程服务暂时不可用, 请稍后重试!", e); } }
private static void addGetParams(String url, Map<String, Object> params, Request.Builder requestBuilder) { StringBuilder paramUrl = new StringBuilder(url); if (params != null && params.size() > 0) { paramUrl.append("?"); params.forEach((k, v) -> paramUrl.append(k + "=" + v + "&")); paramUrl.deleteCharAt(paramUrl.length() - 1); } requestBuilder.url(paramUrl.toString()); }
private static void addPostParams(String url, Map<String, Object> params, Request.Builder requestBuilder, Boolean isFormUrlencode) { RequestBody requestBody; if (params != null && params.size() > 0) { if (isFormUrlencode) { Builder builder = new Builder(); params.forEach((k, v) -> builder.addEncoded(k, v.toString())); requestBody = builder.build(); } else { log.info("json:" + JSON.toJSONString(params)); requestBody = RequestBody.create(JSON_CHARSET, JSON.toJSONString(params)); } } else { requestBody = RequestBody.create(JSON_CHARSET, "{}"); } requestBuilder.post(requestBody); requestBuilder.url(url); }
private static void addHeaders(Map<String, String> headers, Request.Builder requestBuilder) { if (headers == null) { headers = new HashMap<>(4); }
headers.forEach((k, v) -> requestBuilder.addHeader(k, v)); }
}
|