Skip to content

Commit d8aabc5

Browse files
committed
✨ httpImpl 增加默认header
1 parent b5ce379 commit d8aabc5

File tree

6 files changed

+58
-13
lines changed

6 files changed

+58
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<dependency>
1616
<groupId>com.xkcoding.http</groupId>
1717
<artifactId>simple-http</artifactId>
18-
<version>1.0</version>
18+
<version>1.0.2</version>
1919
</dependency>
2020
```
2121

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.1</version>
9+
<version>1.0.2</version>
1010

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

src/main/java/com/xkcoding/http/constants/Constants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,9 @@ public interface Constants {
5757
* 空字符串
5858
*/
5959
String EMPTY = "";
60+
61+
/**
62+
* User-Agent
63+
*/
64+
String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36 simple-http";
6065
}

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import com.xkcoding.http.support.HttpHeader;
2323
import com.xkcoding.http.util.MapUtil;
2424
import com.xkcoding.http.util.StringUtil;
25+
import org.apache.http.Header;
26+
import org.apache.http.HttpHeaders;
2527
import org.apache.http.NameValuePair;
2628
import org.apache.http.client.config.RequestConfig;
2729
import org.apache.http.client.entity.UrlEncodedFormEntity;
@@ -60,6 +62,7 @@ public HttpClientImpl(CloseableHttpClient httpClient) {
6062
}
6163

6264
private String exec(HttpRequestBase request) {
65+
this.addHeader(request);
6366
// 设置超时时长
6467
request.setConfig(RequestConfig.custom()
6568
.setConnectTimeout(Constants.TIMEOUT)
@@ -83,6 +86,19 @@ private String exec(HttpRequestBase request) {
8386
}
8487
}
8588

89+
/**
90+
* 添加request header
91+
*
92+
* @param request HttpRequestBase
93+
*/
94+
private void addHeader(HttpRequestBase request) {
95+
String ua = HttpHeaders.USER_AGENT;
96+
Header[] headers = request.getHeaders(ua);
97+
if (null == headers || headers.length == 0) {
98+
request.addHeader(ua, Constants.USER_AGENT);
99+
}
100+
}
101+
86102
private boolean isSuccess(CloseableHttpResponse response) {
87103
if (response == null) {
88104
return false;

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.xkcoding.http.support.HttpHeader;
2323
import com.xkcoding.http.util.MapUtil;
2424
import com.xkcoding.http.util.StringUtil;
25+
import org.apache.http.HttpHeaders;
2526

2627
import java.io.IOException;
2728
import java.net.URI;
@@ -47,14 +48,25 @@ public HttpClientImpl(HttpClient client) {
4748
this.client = client;
4849
}
4950

50-
private String exec(HttpRequest request) {
51+
private String exec(HttpRequest.Builder builder) {
52+
this.addHeader(builder);
5153
try {
54+
HttpRequest request = builder.build();
5255
return client.send(request, HttpResponse.BodyHandlers.ofString()).body();
5356
} catch (IOException | InterruptedException e) {
5457
throw new SimpleHttpException(e);
5558
}
5659
}
5760

61+
/**
62+
* 添加request header
63+
*
64+
* @param builder HttpRequest.Builder
65+
*/
66+
private void addHeader(HttpRequest.Builder builder) {
67+
builder.header(HttpHeaders.USER_AGENT, Constants.USER_AGENT);
68+
}
69+
5870
/**
5971
* GET 请求
6072
*
@@ -102,7 +114,7 @@ public String get(String url, Map<String, String> params, HttpHeader header, boo
102114
MapUtil.forEach(header.getHeaders(), builder::header);
103115
}
104116

105-
return exec(builder.build());
117+
return exec(builder);
106118
}
107119

108120
/**
@@ -154,7 +166,7 @@ public String post(String url, String data, HttpHeader header) {
154166
MapUtil.forEach(header.getHeaders(), builder::header);
155167
}
156168

157-
return this.exec(builder.build());
169+
return this.exec(builder);
158170
}
159171

160172
/**

src/main/java/com/xkcoding/http/support/okhttp3/OkHttp3Impl.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.xkcoding.http.util.MapUtil;
2424
import com.xkcoding.http.util.StringUtil;
2525
import okhttp3.*;
26+
import org.apache.http.HttpHeaders;
2627

2728
import java.io.IOException;
2829
import java.time.Duration;
@@ -37,8 +38,8 @@
3738
* @date Created in 2019/12/24 19:06
3839
*/
3940
public class OkHttp3Impl implements Http {
40-
private final OkHttpClient httpClient;
4141
public static final MediaType CONTENT_TYPE_JSON = MediaType.get(Constants.CONTENT_TYPE_JSON);
42+
private final OkHttpClient httpClient;
4243

4344

4445
public OkHttp3Impl() {
@@ -53,7 +54,9 @@ public OkHttp3Impl(OkHttpClient httpClient) {
5354
this.httpClient = httpClient;
5455
}
5556

56-
private String exec(Request request) {
57+
private String exec(Request.Builder requestBuilder) {
58+
this.addHeader(requestBuilder);
59+
Request request = requestBuilder.build();
5760
try (Response response = httpClient.newCall(request).execute()) {
5861
if (!response.isSuccessful()) {
5962
throw new SimpleHttpException("Unexpected code " + response);
@@ -65,6 +68,15 @@ private String exec(Request request) {
6568
}
6669
}
6770

71+
/**
72+
* 添加request header
73+
*
74+
* @param builder Request.Builder
75+
*/
76+
private void addHeader(Request.Builder builder) {
77+
builder.header(HttpHeaders.USER_AGENT, Constants.USER_AGENT);
78+
}
79+
6880
/**
6981
* GET 请求
7082
*
@@ -112,9 +124,9 @@ public String get(String url, Map<String, String> params, HttpHeader header, boo
112124
if (header != null) {
113125
MapUtil.forEach(header.getHeaders(), requestBuilder::addHeader);
114126
}
115-
Request request = requestBuilder.get().build();
127+
requestBuilder = requestBuilder.get();
116128

117-
return exec(request);
129+
return exec(requestBuilder);
118130
}
119131

120132
/**
@@ -159,9 +171,9 @@ public String post(String url, String data, HttpHeader header) {
159171
if (header != null) {
160172
MapUtil.forEach(header.getHeaders(), requestBuilder::addHeader);
161173
}
162-
Request request = requestBuilder.post(body).build();
174+
requestBuilder = requestBuilder.post(body);
163175

164-
return exec(request);
176+
return exec(requestBuilder);
165177
}
166178

167179
/**
@@ -200,8 +212,8 @@ public String post(String url, Map<String, String> params, HttpHeader header, bo
200212
if (header != null) {
201213
MapUtil.forEach(header.getHeaders(), requestBuilder::addHeader);
202214
}
203-
Request request = requestBuilder.post(body).build();
215+
requestBuilder = requestBuilder.post(body);
204216

205-
return exec(request);
217+
return exec(requestBuilder);
206218
}
207219
}

0 commit comments

Comments
 (0)