Skip to content

Commit 0a979b0

Browse files
authored
封装请求响应 (#11)
* 封装请求响应。 1. 添加 simpleHttpResponse 类,包含 code(响应码),success(是否成功),header(响应头),body(响应体)字段,用于封装 http 响应 2. 不成功的 http 响应不再抛出 “Unexpected code”异常。而是封装为 simpleHttpResponse 类,使用者自行处理失败的请求响应
1 parent f104bed commit 0a979b0

File tree

10 files changed

+198
-71
lines changed

10 files changed

+198
-71
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.xkcoding.http</groupId>
88
<artifactId>simple-http</artifactId>
9-
<version>1.0.3</version>
9+
<version>1.0.4</version>
1010

1111
<name>${project.artifactId}</name>
1212
<url>https://github.com/xkcoding/simple-http</url>

src/main/java/com/xkcoding/http/HttpUtil.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.xkcoding.http.support.AbstractHttp;
2323
import com.xkcoding.http.support.Http;
2424
import com.xkcoding.http.support.HttpHeader;
25+
import com.xkcoding.http.support.SimpleHttpResponse;
2526
import com.xkcoding.http.util.ClassUtil;
2627
import lombok.experimental.UtilityClass;
2728

@@ -98,7 +99,7 @@ public void setConfig(HttpConfig httpConfig) {
9899
* @param url URL
99100
* @return 结果
100101
*/
101-
public String get(String url) {
102+
public SimpleHttpResponse get(String url) {
102103
checkHttpNotNull(proxy);
103104
return proxy.get(url);
104105
}
@@ -111,7 +112,7 @@ public String get(String url) {
111112
* @param encode 是否需要 url encode
112113
* @return 结果
113114
*/
114-
public String get(String url, Map<String, String> params, boolean encode) {
115+
public SimpleHttpResponse get(String url, Map<String, String> params, boolean encode) {
115116
checkHttpNotNull(proxy);
116117
return proxy.get(url, params, encode);
117118
}
@@ -125,7 +126,7 @@ public String get(String url, Map<String, String> params, boolean encode) {
125126
* @param encode 是否需要 url encode
126127
* @return 结果
127128
*/
128-
public String get(String url, Map<String, String> params, HttpHeader header, boolean encode) {
129+
public SimpleHttpResponse get(String url, Map<String, String> params, HttpHeader header, boolean encode) {
129130
checkHttpNotNull(proxy);
130131
return proxy.get(url, params, header, encode);
131132
}
@@ -136,7 +137,7 @@ public String get(String url, Map<String, String> params, HttpHeader header, boo
136137
* @param url URL
137138
* @return 结果
138139
*/
139-
public String post(String url) {
140+
public SimpleHttpResponse post(String url) {
140141
checkHttpNotNull(proxy);
141142
return proxy.post(url);
142143
}
@@ -148,7 +149,7 @@ public String post(String url) {
148149
* @param data JSON 参数
149150
* @return 结果
150151
*/
151-
public String post(String url, String data) {
152+
public SimpleHttpResponse post(String url, String data) {
152153
checkHttpNotNull(proxy);
153154
return proxy.post(url, data);
154155
}
@@ -161,7 +162,7 @@ public String post(String url, String data) {
161162
* @param header 请求头
162163
* @return 结果
163164
*/
164-
public String post(String url, String data, HttpHeader header) {
165+
public SimpleHttpResponse post(String url, String data, HttpHeader header) {
165166
checkHttpNotNull(proxy);
166167
return proxy.post(url, data, header);
167168
}
@@ -174,7 +175,7 @@ public String post(String url, String data, HttpHeader header) {
174175
* @param encode 是否需要 url encode
175176
* @return 结果
176177
*/
177-
public String post(String url, Map<String, String> params, boolean encode) {
178+
public SimpleHttpResponse post(String url, Map<String, String> params, boolean encode) {
178179
checkHttpNotNull(proxy);
179180
return proxy.post(url, params, encode);
180181
}
@@ -188,7 +189,7 @@ public String post(String url, Map<String, String> params, boolean encode) {
188189
* @param encode 是否需要 url encode
189190
* @return 结果
190191
*/
191-
public String post(String url, Map<String, String> params, HttpHeader header, boolean encode) {
192+
public SimpleHttpResponse post(String url, Map<String, String> params, HttpHeader header, boolean encode) {
192193
checkHttpNotNull(proxy);
193194
return proxy.post(url, params, header, encode);
194195
}

src/main/java/com/xkcoding/http/support/Http.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public interface Http {
3333
* @param url URL
3434
* @return 结果
3535
*/
36-
String get(String url);
36+
SimpleHttpResponse get(String url);
3737

3838
/**
3939
* GET 请求
@@ -43,7 +43,7 @@ public interface Http {
4343
* @param encode 是否需要 url encode
4444
* @return 结果
4545
*/
46-
String get(String url, Map<String, String> params, boolean encode);
46+
SimpleHttpResponse get(String url, Map<String, String> params, boolean encode);
4747

4848
/**
4949
* GET 请求
@@ -54,15 +54,15 @@ public interface Http {
5454
* @param encode 是否需要 url encode
5555
* @return 结果
5656
*/
57-
String get(String url, Map<String, String> params, HttpHeader header, boolean encode);
57+
SimpleHttpResponse get(String url, Map<String, String> params, HttpHeader header, boolean encode);
5858

5959
/**
6060
* POST 请求
6161
*
6262
* @param url URL
6363
* @return 结果
6464
*/
65-
String post(String url);
65+
SimpleHttpResponse post(String url);
6666

6767
/**
6868
* POST 请求
@@ -71,7 +71,7 @@ public interface Http {
7171
* @param data JSON 参数
7272
* @return 结果
7373
*/
74-
String post(String url, String data);
74+
SimpleHttpResponse post(String url, String data);
7575

7676
/**
7777
* POST 请求
@@ -81,7 +81,7 @@ public interface Http {
8181
* @param header 请求头
8282
* @return 结果
8383
*/
84-
String post(String url, String data, HttpHeader header);
84+
SimpleHttpResponse post(String url, String data, HttpHeader header);
8585

8686
/**
8787
* POST 请求
@@ -91,7 +91,7 @@ public interface Http {
9191
* @param encode 是否需要 url encode
9292
* @return 结果
9393
*/
94-
String post(String url, Map<String, String> params, boolean encode);
94+
SimpleHttpResponse post(String url, Map<String, String> params, boolean encode);
9595

9696
/**
9797
* POST 请求
@@ -102,5 +102,5 @@ public interface Http {
102102
* @param encode 是否需要 url encode
103103
* @return 结果
104104
*/
105-
String post(String url, Map<String, String> params, HttpHeader header, boolean encode);
105+
SimpleHttpResponse post(String url, Map<String, String> params, HttpHeader header, boolean encode);
106106
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.xkcoding.http.support;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
/**
7+
* <p>
8+
* 响应封装
9+
* </p>
10+
*
11+
* @author zhihai.yu
12+
* @date Created in 2021/09/10 20:24
13+
*/
14+
public class SimpleHttpResponse {
15+
private boolean success;
16+
private int code;
17+
private Map<String, List<String>> headers;
18+
private String body;
19+
20+
public SimpleHttpResponse() {
21+
}
22+
23+
public SimpleHttpResponse(boolean success, int code, Map<String, List<String>> headers, String body) {
24+
this.success = success;
25+
this.code = code;
26+
this.headers = headers;
27+
this.body = body;
28+
}
29+
30+
public Map<String, List<String>> getHeaders() {
31+
return headers;
32+
}
33+
34+
public SimpleHttpResponse setHeaders(Map<String, List<String>> headers) {
35+
this.headers = headers;
36+
return this;
37+
}
38+
39+
public String getBody() {
40+
return body;
41+
}
42+
43+
public SimpleHttpResponse setBody(String body) {
44+
this.body = body;
45+
return this;
46+
}
47+
48+
public boolean isSuccess() {
49+
return success;
50+
}
51+
52+
public SimpleHttpResponse setSuccess(boolean success) {
53+
this.success = success;
54+
return this;
55+
}
56+
57+
public int getCode() {
58+
return code;
59+
}
60+
61+
public SimpleHttpResponse setCode(int code) {
62+
this.code = code;
63+
return this;
64+
}
65+
66+
@Override
67+
public String toString() {
68+
return "SimpleHttpResponse{" +
69+
"success=" + success +
70+
", code=" + code +
71+
", headers=" + headers +
72+
", body='" + body + '\'' +
73+
'}';
74+
}
75+
}

src/main/java/com/xkcoding/http/support/httpclient/HttpClientImpl.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.xkcoding.http.support.httpclient;
1818

19+
import com.xkcoding.http.support.SimpleHttpResponse;
1920
import com.xkcoding.http.config.HttpConfig;
2021
import com.xkcoding.http.constants.Constants;
2122
import com.xkcoding.http.exception.SimpleHttpException;
@@ -24,6 +25,7 @@
2425
import com.xkcoding.http.util.MapUtil;
2526
import com.xkcoding.http.util.StringUtil;
2627
import org.apache.http.Header;
28+
import org.apache.http.HeaderElement;
2729
import org.apache.http.HttpHost;
2830
import org.apache.http.NameValuePair;
2931
import org.apache.http.client.config.RequestConfig;
@@ -42,8 +44,10 @@
4244
import java.net.InetSocketAddress;
4345
import java.net.Proxy;
4446
import java.util.ArrayList;
47+
import java.util.Arrays;
4548
import java.util.List;
4649
import java.util.Map;
50+
import java.util.stream.Collectors;
4751

4852
/**
4953
* <p>
@@ -69,7 +73,7 @@ public HttpClientImpl(CloseableHttpClient httpClient, HttpConfig httpConfig) {
6973
this.httpClient = httpClient;
7074
}
7175

72-
private String exec(HttpRequestBase request) {
76+
private SimpleHttpResponse exec(HttpRequestBase request) {
7377
this.addHeader(request);
7478
// 设置超时时长
7579
RequestConfig.Builder configBuilder = RequestConfig.custom().setConnectTimeout(httpConfig.getTimeout()).setSocketTimeout(httpConfig.getTimeout()).setConnectionRequestTimeout(httpConfig.getTimeout());
@@ -84,21 +88,27 @@ private String exec(HttpRequestBase request) {
8488
request.setConfig(configBuilder.build());
8589

8690
try (CloseableHttpResponse response = this.httpClient.execute(request)) {
87-
if (!isSuccess(response)) {
88-
throw new SimpleHttpException("Unexpected code " + response);
89-
}
9091

9192
StringBuffer body = new StringBuffer();
9293
if (response.getEntity() != null) {
9394
body.append(EntityUtils.toString(response.getEntity(), Constants.DEFAULT_ENCODING));
9495
}
9596

96-
return body.toString();
97+
int code = response.getStatusLine().getStatusCode();
98+
boolean success = isSuccess(response);
99+
Map<String, List<String>> headers = Arrays.stream(response.getAllHeaders()).collect(Collectors.toMap(Header::getName, (value) -> {
100+
ArrayList<String> headerValue = new ArrayList<>();
101+
headerValue.add(value.getValue());
102+
return headerValue;
103+
},(oldValue,newValue)->newValue));
104+
return new SimpleHttpResponse(success,code,headers, body.toString());
97105
} catch (IOException e) {
98-
throw new SimpleHttpException(e);
106+
e.printStackTrace();
107+
return new SimpleHttpResponse(false,400,null,null);
99108
}
100109
}
101110

111+
102112
/**
103113
* 添加request header
104114
*
@@ -129,7 +139,7 @@ private boolean isSuccess(CloseableHttpResponse response) {
129139
* @return 结果
130140
*/
131141
@Override
132-
public String get(String url) {
142+
public SimpleHttpResponse get(String url) {
133143
return this.get(url, null, false);
134144
}
135145

@@ -142,7 +152,7 @@ public String get(String url) {
142152
* @return 结果
143153
*/
144154
@Override
145-
public String get(String url, Map<String, String> params, boolean encode) {
155+
public SimpleHttpResponse get(String url, Map<String, String> params, boolean encode) {
146156
return this.get(url, params, null, encode);
147157
}
148158

@@ -156,7 +166,7 @@ public String get(String url, Map<String, String> params, boolean encode) {
156166
* @return 结果
157167
*/
158168
@Override
159-
public String get(String url, Map<String, String> params, HttpHeader header, boolean encode) {
169+
public SimpleHttpResponse get(String url, Map<String, String> params, HttpHeader header, boolean encode) {
160170
String baseUrl = StringUtil.appendIfNotContain(url, "?", "&");
161171
url = baseUrl + MapUtil.parseMapToString(params, encode);
162172

@@ -176,7 +186,7 @@ public String get(String url, Map<String, String> params, HttpHeader header, boo
176186
* @return 结果
177187
*/
178188
@Override
179-
public String post(String url) {
189+
public SimpleHttpResponse post(String url) {
180190
HttpPost httpPost = new HttpPost(url);
181191
return this.exec(httpPost);
182192
}
@@ -189,7 +199,7 @@ public String post(String url) {
189199
* @return 结果
190200
*/
191201
@Override
192-
public String post(String url, String data) {
202+
public SimpleHttpResponse post(String url, String data) {
193203
return this.post(url, data, null);
194204
}
195205

@@ -202,7 +212,7 @@ public String post(String url, String data) {
202212
* @return 结果
203213
*/
204214
@Override
205-
public String post(String url, String data, HttpHeader header) {
215+
public SimpleHttpResponse post(String url, String data, HttpHeader header) {
206216
HttpPost httpPost = new HttpPost(url);
207217

208218
if (StringUtil.isNotEmpty(data)) {
@@ -228,7 +238,7 @@ public String post(String url, String data, HttpHeader header) {
228238
* @return 结果
229239
*/
230240
@Override
231-
public String post(String url, Map<String, String> params, boolean encode) {
241+
public SimpleHttpResponse post(String url, Map<String, String> params, boolean encode) {
232242
return this.post(url, params, null, encode);
233243
}
234244

@@ -242,7 +252,7 @@ public String post(String url, Map<String, String> params, boolean encode) {
242252
* @return 结果
243253
*/
244254
@Override
245-
public String post(String url, Map<String, String> params, HttpHeader header, boolean encode) {
255+
public SimpleHttpResponse post(String url, Map<String, String> params, HttpHeader header, boolean encode) {
246256
HttpPost httpPost = new HttpPost(url);
247257

248258
if (MapUtil.isNotEmpty(params)) {

0 commit comments

Comments
 (0)