Skip to content

Commit

Permalink
✨ httpImpl 增加默认header
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyd-c committed Apr 9, 2020
1 parent b5ce379 commit d8aabc5
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 13 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</version>
<version>1.0.2</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.1</version>
<version>1.0.2</version>

<name>${project.artifactId}</name>
<url>https://github.com/xkcoding/simple-http</url>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/xkcoding/http/constants/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ public interface Constants {
* 空字符串
*/
String EMPTY = "";

/**
* User-Agent
*/
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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.xkcoding.http.support.HttpHeader;
import com.xkcoding.http.util.MapUtil;
import com.xkcoding.http.util.StringUtil;
import org.apache.http.Header;
import org.apache.http.HttpHeaders;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
Expand Down Expand Up @@ -60,6 +62,7 @@ public HttpClientImpl(CloseableHttpClient httpClient) {
}

private String exec(HttpRequestBase request) {
this.addHeader(request);
// 设置超时时长
request.setConfig(RequestConfig.custom()
.setConnectTimeout(Constants.TIMEOUT)
Expand All @@ -83,6 +86,19 @@ private String exec(HttpRequestBase request) {
}
}

/**
* 添加request header
*
* @param request HttpRequestBase
*/
private void addHeader(HttpRequestBase request) {
String ua = HttpHeaders.USER_AGENT;
Header[] headers = request.getHeaders(ua);
if (null == headers || headers.length == 0) {
request.addHeader(ua, Constants.USER_AGENT);
}
}

private boolean isSuccess(CloseableHttpResponse response) {
if (response == null) {
return false;
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/com/xkcoding/http/support/java11/HttpClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.xkcoding.http.support.HttpHeader;
import com.xkcoding.http.util.MapUtil;
import com.xkcoding.http.util.StringUtil;
import org.apache.http.HttpHeaders;

import java.io.IOException;
import java.net.URI;
Expand All @@ -47,14 +48,25 @@ public HttpClientImpl(HttpClient client) {
this.client = client;
}

private String exec(HttpRequest request) {
private String exec(HttpRequest.Builder builder) {
this.addHeader(builder);
try {
HttpRequest request = builder.build();
return client.send(request, HttpResponse.BodyHandlers.ofString()).body();
} catch (IOException | InterruptedException e) {
throw new SimpleHttpException(e);
}
}

/**
* 添加request header
*
* @param builder HttpRequest.Builder
*/
private void addHeader(HttpRequest.Builder builder) {
builder.header(HttpHeaders.USER_AGENT, Constants.USER_AGENT);
}

/**
* GET 请求
*
Expand Down Expand Up @@ -102,7 +114,7 @@ public String get(String url, Map<String, String> params, HttpHeader header, boo
MapUtil.forEach(header.getHeaders(), builder::header);
}

return exec(builder.build());
return exec(builder);
}

/**
Expand Down Expand Up @@ -154,7 +166,7 @@ public String post(String url, String data, HttpHeader header) {
MapUtil.forEach(header.getHeaders(), builder::header);
}

return this.exec(builder.build());
return this.exec(builder);
}

/**
Expand Down
28 changes: 20 additions & 8 deletions src/main/java/com/xkcoding/http/support/okhttp3/OkHttp3Impl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.xkcoding.http.util.MapUtil;
import com.xkcoding.http.util.StringUtil;
import okhttp3.*;
import org.apache.http.HttpHeaders;

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


public OkHttp3Impl() {
Expand All @@ -53,7 +54,9 @@ public OkHttp3Impl(OkHttpClient httpClient) {
this.httpClient = httpClient;
}

private String exec(Request request) {
private String exec(Request.Builder requestBuilder) {
this.addHeader(requestBuilder);
Request request = requestBuilder.build();
try (Response response = httpClient.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new SimpleHttpException("Unexpected code " + response);
Expand All @@ -65,6 +68,15 @@ private String exec(Request request) {
}
}

/**
* 添加request header
*
* @param builder Request.Builder
*/
private void addHeader(Request.Builder builder) {
builder.header(HttpHeaders.USER_AGENT, Constants.USER_AGENT);
}

/**
* GET 请求
*
Expand Down Expand Up @@ -112,9 +124,9 @@ public String get(String url, Map<String, String> params, HttpHeader header, boo
if (header != null) {
MapUtil.forEach(header.getHeaders(), requestBuilder::addHeader);
}
Request request = requestBuilder.get().build();
requestBuilder = requestBuilder.get();

return exec(request);
return exec(requestBuilder);
}

/**
Expand Down Expand Up @@ -159,9 +171,9 @@ public String post(String url, String data, HttpHeader header) {
if (header != null) {
MapUtil.forEach(header.getHeaders(), requestBuilder::addHeader);
}
Request request = requestBuilder.post(body).build();
requestBuilder = requestBuilder.post(body);

return exec(request);
return exec(requestBuilder);
}

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

return exec(request);
return exec(requestBuilder);
}
}

0 comments on commit d8aabc5

Please sign in to comment.