This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 0ce946fc66 optimized code (#5027)
0ce946fc66 is described below
commit 0ce946fc6679a1aa986b97668878e2e720096f49
Author: Kerwin Bryant <[email protected]>
AuthorDate: Thu Aug 17 14:07:39 2023 +0800
optimized code (#5027)
Requests other than 200 should not respond normally
Co-authored-by: 杨文杰 <[email protected]>
Co-authored-by: Liming Deng <[email protected]>
---
.../org/apache/shenyu/admin/utils/HttpUtils.java | 33 ++++++++++------------
1 file changed, 15 insertions(+), 18 deletions(-)
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/HttpUtils.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/HttpUtils.java
index 7a484cb535..be28553dbe 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/HttpUtils.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/HttpUtils.java
@@ -29,6 +29,7 @@ import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.apache.commons.codec.digest.DigestUtils;
+import org.apache.http.HttpStatus;
import java.io.ByteArrayOutputStream;
import java.io.File;
@@ -177,10 +178,7 @@ public class HttpUtils {
public String get(final String url, final Map<String, String> header)
throws IOException {
Request.Builder builder = new Request.Builder().url(url).get();
addHeader(builder, header);
-
- Request request = builder.build();
- Response response = httpClient.newCall(request).execute();
- return response.body().string();
+ return reqString(builder.build());
}
/**
@@ -197,13 +195,7 @@ public class HttpUtils {
final HTTPMethod method) throws IOException {
Request.Builder requestBuilder = buildRequestBuilder(url, form,
method);
addHeader(requestBuilder, header);
-
- Request request = requestBuilder.build();
- try (Response response = httpClient
- .newCall(request)
- .execute()) {
- return response.body().string();
- }
+ return reqString(requestBuilder.build());
}
/**
@@ -222,13 +214,7 @@ public class HttpUtils {
.url(url)
.post(body);
addHeader(requestBuilder, header);
-
- Request request = requestBuilder.build();
- try (Response response = httpClient
- .newCall(request)
- .execute()) {
- return response.body().string();
- }
+ return reqString(requestBuilder.build());
}
/**
@@ -375,6 +361,17 @@ public class HttpUtils {
}
}
+ private String reqString(final Request request) throws IOException {
+ try (Response response = httpClient
+ .newCall(request)
+ .execute()) {
+ if (response.code() != HttpStatus.SC_OK) {
+ throw new IOException(response.toString());
+ }
+ return response.body().string();
+ }
+ }
+
public enum HTTPMethod {
GET,
POST,