diff --git a/README.md b/README.md index 5e7ede4..7bd87a9 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ com.xkcoding.http simple-http - 1.0.4 + 1.0.5 ``` diff --git a/pom.xml b/pom.xml index b7b071d..881c1f3 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.xkcoding.http simple-http - 1.0.4 + 1.0.5 ${project.artifactId} https://github.com/xkcoding/simple-http diff --git a/src/main/java/com/xkcoding/http/support/SimpleHttpResponse.java b/src/main/java/com/xkcoding/http/support/SimpleHttpResponse.java index 2a187f5..1dac403 100644 --- a/src/main/java/com/xkcoding/http/support/SimpleHttpResponse.java +++ b/src/main/java/com/xkcoding/http/support/SimpleHttpResponse.java @@ -23,4 +23,5 @@ public class SimpleHttpResponse { private int code; private Map> headers; private String body; + private String error; } diff --git a/src/main/java/com/xkcoding/http/support/httpclient/HttpClientImpl.java b/src/main/java/com/xkcoding/http/support/httpclient/HttpClientImpl.java index dd95e2a..4d2f1e5 100644 --- a/src/main/java/com/xkcoding/http/support/httpclient/HttpClientImpl.java +++ b/src/main/java/com/xkcoding/http/support/httpclient/HttpClientImpl.java @@ -16,16 +16,14 @@ package com.xkcoding.http.support.httpclient; -import com.xkcoding.http.support.SimpleHttpResponse; import com.xkcoding.http.config.HttpConfig; import com.xkcoding.http.constants.Constants; -import com.xkcoding.http.exception.SimpleHttpException; import com.xkcoding.http.support.AbstractHttp; import com.xkcoding.http.support.HttpHeader; +import com.xkcoding.http.support.SimpleHttpResponse; import com.xkcoding.http.util.MapUtil; import com.xkcoding.http.util.StringUtil; import org.apache.http.Header; -import org.apache.http.HeaderElement; import org.apache.http.HttpHost; import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; @@ -40,7 +38,6 @@ import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; -import java.io.IOException; import java.net.InetSocketAddress; import java.net.Proxy; import java.util.ArrayList; @@ -95,16 +92,16 @@ private SimpleHttpResponse exec(HttpRequestBase request) { } int code = response.getStatusLine().getStatusCode(); - boolean success = isSuccess(response); + boolean successful = isSuccess(response); Map> headers = Arrays.stream(response.getAllHeaders()).collect(Collectors.toMap(Header::getName, (value) -> { ArrayList headerValue = new ArrayList<>(); headerValue.add(value.getValue()); return headerValue; - },(oldValue,newValue)->newValue)); - return new SimpleHttpResponse(success,code,headers, body.toString()); - } catch (IOException e) { + }, (oldValue, newValue) -> newValue)); + return new SimpleHttpResponse(successful, code, headers, body.toString(), null); + } catch (Exception e) { e.printStackTrace(); - return new SimpleHttpResponse(false,400,null,null); + return new SimpleHttpResponse(false, 500, null, null, e.getMessage()); } } diff --git a/src/main/java/com/xkcoding/http/support/hutool/HutoolImpl.java b/src/main/java/com/xkcoding/http/support/hutool/HutoolImpl.java index 38b21a6..e5cf542 100644 --- a/src/main/java/com/xkcoding/http/support/hutool/HutoolImpl.java +++ b/src/main/java/com/xkcoding/http/support/hutool/HutoolImpl.java @@ -18,16 +18,14 @@ import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; -import com.xkcoding.http.exception.SimpleHttpException; -import com.xkcoding.http.support.SimpleHttpResponse; import com.xkcoding.http.config.HttpConfig; import com.xkcoding.http.support.AbstractHttp; import com.xkcoding.http.support.HttpHeader; +import com.xkcoding.http.support.SimpleHttpResponse; import com.xkcoding.http.util.MapUtil; import com.xkcoding.http.util.StringUtil; import com.xkcoding.http.util.UrlUtil; -import java.io.IOException; import java.util.List; import java.util.Map; @@ -61,10 +59,10 @@ private SimpleHttpResponse exec(HttpRequest request) { boolean successful = response.isOk(); String body = response.body(); Map> headers = response.headers(); - return new SimpleHttpResponse(successful, code, headers, body); - }catch (RuntimeException e){ + return new SimpleHttpResponse(successful, code, headers, body, null); + } catch (Exception e) { e.printStackTrace(); - return new SimpleHttpResponse(false,400,null,null); + return new SimpleHttpResponse(false, 500, null, null, e.getMessage()); } } diff --git a/src/main/java/com/xkcoding/http/support/java11/HttpClientImpl.java b/src/main/java/com/xkcoding/http/support/java11/HttpClientImpl.java index 4a45df2..2642a7c 100644 --- a/src/main/java/com/xkcoding/http/support/java11/HttpClientImpl.java +++ b/src/main/java/com/xkcoding/http/support/java11/HttpClientImpl.java @@ -16,16 +16,14 @@ package com.xkcoding.http.support.java11; -import com.xkcoding.http.support.SimpleHttpResponse; import com.xkcoding.http.config.HttpConfig; import com.xkcoding.http.constants.Constants; -import com.xkcoding.http.exception.SimpleHttpException; import com.xkcoding.http.support.AbstractHttp; import com.xkcoding.http.support.HttpHeader; +import com.xkcoding.http.support.SimpleHttpResponse; import com.xkcoding.http.util.MapUtil; import com.xkcoding.http.util.StringUtil; -import java.io.IOException; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; @@ -69,17 +67,17 @@ private SimpleHttpResponse exec(HttpRequest.Builder builder) { HttpResponse httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString()); int code = httpResponse.statusCode(); - boolean success = isSuccess(httpResponse); + boolean successful = isSuccess(httpResponse); Map> headers = httpResponse.headers().map(); String body = httpResponse.body(); - return new SimpleHttpResponse(success,code,headers,body); - } catch (IOException | InterruptedException e) { + return new SimpleHttpResponse(successful, code, headers, body, null); + } catch (Exception e) { e.printStackTrace(); - return new SimpleHttpResponse(false,400,null,null); + return new SimpleHttpResponse(false, 500, null, null, e.getMessage()); } } - private boolean isSuccess(HttpResponse response) { + private boolean isSuccess(HttpResponse response) { if (response == null) { return false; } diff --git a/src/main/java/com/xkcoding/http/support/okhttp3/OkHttp3Impl.java b/src/main/java/com/xkcoding/http/support/okhttp3/OkHttp3Impl.java index 9bf6080..236d8e6 100644 --- a/src/main/java/com/xkcoding/http/support/okhttp3/OkHttp3Impl.java +++ b/src/main/java/com/xkcoding/http/support/okhttp3/OkHttp3Impl.java @@ -16,17 +16,15 @@ package com.xkcoding.http.support.okhttp3; -import com.xkcoding.http.support.SimpleHttpResponse; import com.xkcoding.http.config.HttpConfig; import com.xkcoding.http.constants.Constants; -import com.xkcoding.http.exception.SimpleHttpException; import com.xkcoding.http.support.AbstractHttp; import com.xkcoding.http.support.HttpHeader; +import com.xkcoding.http.support.SimpleHttpResponse; import com.xkcoding.http.util.MapUtil; import com.xkcoding.http.util.StringUtil; import okhttp3.*; -import java.io.IOException; import java.time.Duration; import java.util.List; import java.util.Map; @@ -73,11 +71,12 @@ private SimpleHttpResponse exec(Request.Builder requestBuilder) { int code = response.code(); boolean successful = response.isSuccessful(); Map> headers = response.headers().toMultimap(); - String body = response.body().string(); - return new SimpleHttpResponse(successful,code,headers,body); - } catch (IOException e) { + ResponseBody responseBody = response.body(); + String body = null == responseBody ? null : responseBody.string(); + return new SimpleHttpResponse(successful, code, headers, body, null); + } catch (Exception e) { e.printStackTrace(); - return new SimpleHttpResponse(false,400,null,null); + return new SimpleHttpResponse(false, 500, null, null, e.getMessage()); } }