Skip to content

Commit

Permalink
🎨 格式化代码/异常代码改为500/返回异常信息 (#12)
Browse files Browse the repository at this point in the history
* 🎨 格式化代码/异常代码改为500/返回异常信息
  • Loading branch information
zhangyd-c authored Sep 22, 2021
1 parent 0313341 commit 1bd84f0
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<dependency>
<groupId>com.xkcoding.http</groupId>
<artifactId>simple-http</artifactId>
<version>1.0.4</version>
<version>1.0.5</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.xkcoding.http</groupId>
<artifactId>simple-http</artifactId>
<version>1.0.4</version>
<version>1.0.5</version>

<name>${project.artifactId}</name>
<url>https://github.com/xkcoding/simple-http</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ public class SimpleHttpResponse {
private int code;
private Map<String, List<String>> headers;
private String body;
private String error;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -95,16 +92,16 @@ private SimpleHttpResponse exec(HttpRequestBase request) {
}

int code = response.getStatusLine().getStatusCode();
boolean success = isSuccess(response);
boolean successful = isSuccess(response);
Map<String, List<String>> headers = Arrays.stream(response.getAllHeaders()).collect(Collectors.toMap(Header::getName, (value) -> {
ArrayList<String> 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());
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/main/java/com/xkcoding/http/support/hutool/HutoolImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -61,10 +59,10 @@ private SimpleHttpResponse exec(HttpRequest request) {
boolean successful = response.isOk();
String body = response.body();
Map<String, List<String>> 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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,17 +67,17 @@ private SimpleHttpResponse exec(HttpRequest.Builder builder) {
HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());

int code = httpResponse.statusCode();
boolean success = isSuccess(httpResponse);
boolean successful = isSuccess(httpResponse);
Map<String, List<String>> 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<String> response) {
if (response == null) {
return false;
}
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/com/xkcoding/http/support/okhttp3/OkHttp3Impl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,11 +71,12 @@ private SimpleHttpResponse exec(Request.Builder requestBuilder) {
int code = response.code();
boolean successful = response.isSuccessful();
Map<String, List<String>> 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());
}
}

Expand Down

0 comments on commit 1bd84f0

Please sign in to comment.