Update HttpClientUtil.java

修正:responseBody没有关闭导致内存泄漏的问题
修正:抛出异常不够具体
pull/40/head
DJ 3 years ago committed by GitHub
parent 4e507e6bc2
commit 327cf4dd3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -165,23 +165,29 @@ public class HttpClientUtil {
.url(url) .url(url)
.post(requestBody) .post(requestBody)
.build(); .build();
Response resp = hippo4JOkHttpClient.newCall(request).execute(); try (Response resp = hippo4JOkHttpClient.newCall(request).execute()) {
if (resp.code() != HTTP_OK_CODE) { try (ResponseBody responseBody = resp.body()) {
String msg = String.format("HttpPost 响应 code 异常. [code] %s [url] %s [body] %s", resp.code(), url, jsonBody); if (resp.code() != HTTP_OK_CODE) {
throw new RuntimeException(msg); String msg = String.format("HttpPost 响应 code 异常. [code] %s [url] %s [body] %s", resp.code(), url, jsonBody);
throw new ResponseStatusException(HttpStatus.valueOf(resp.code()), msg);
}
return resp.body().string();
}
} }
return resp.body().string();
} }
@SneakyThrows @SneakyThrows
private byte[] doGet(String url) { private byte[] doGet(String url) {
Request request = new Request.Builder().get().url(url).build(); Request request = new Request.Builder().get().url(url).build();
Response resp = hippo4JOkHttpClient.newCall(request).execute(); try(Response resp = hippo4JOkHttpClient.newCall(request).execute()){
if (resp.code() != HTTP_OK_CODE) { try(ResponseBody responseBody = resp.body()){
String msg = String.format("HttpGet 响应 code 异常. [code] %s [url] %s", resp.code(), url); if (resp.code() != HTTP_OK_CODE) {
throw new RuntimeException(msg); String msg = String.format("HttpGet 响应 code 异常. [code] %s [url] %s", resp.code(), url);
throw new ResponseStatusException(HttpStatus.valueOf(resp.code()), msg);
}
return resp.body().bytes();
}
} }
return resp.body().bytes();
} }
@SneakyThrows @SneakyThrows

Loading…
Cancel
Save