Merge pull request #40 from smartdj/fix_HttpClientUtil_memory_leak

修正HttpClientUtil中的内存泄漏以及优化抛出异常
pull/41/head
longtai 3 years ago committed by GitHub
commit f71e49ff44
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -165,23 +165,29 @@ public class HttpClientUtil {
.url(url)
.post(requestBody)
.build();
Response resp = hippo4JOkHttpClient.newCall(request).execute();
if (resp.code() != HTTP_OK_CODE) {
String msg = String.format("HttpPost 响应 code 异常. [code] %s [url] %s [body] %s", resp.code(), url, jsonBody);
throw new RuntimeException(msg);
try (Response resp = hippo4JOkHttpClient.newCall(request).execute()) {
try (ResponseBody responseBody = resp.body()) {
if (resp.code() != HTTP_OK_CODE) {
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
private byte[] doGet(String url) {
Request request = new Request.Builder().get().url(url).build();
Response resp = hippo4JOkHttpClient.newCall(request).execute();
if (resp.code() != HTTP_OK_CODE) {
String msg = String.format("HttpGet 响应 code 异常. [code] %s [url] %s", resp.code(), url);
throw new RuntimeException(msg);
try(Response resp = hippo4JOkHttpClient.newCall(request).execute()){
try(ResponseBody responseBody = resp.body()){
if (resp.code() != HTTP_OK_CODE) {
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

Loading…
Cancel
Save