diff --git a/README.md b/README.md index 370d1c0..5a5153c 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ com.xkcoding.http simple-http - 1.0 + 1.0.2 ``` diff --git a/pom.xml b/pom.xml index b902027..f78eedc 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.xkcoding.http simple-http - 1.0.1 + 1.0.2 ${project.artifactId} https://github.com/xkcoding/simple-http diff --git a/src/main/java/com/xkcoding/http/constants/Constants.java b/src/main/java/com/xkcoding/http/constants/Constants.java index 932afc1..7ec2705 100644 --- a/src/main/java/com/xkcoding/http/constants/Constants.java +++ b/src/main/java/com/xkcoding/http/constants/Constants.java @@ -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"; } 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 d32b8cf..878ca98 100644 --- a/src/main/java/com/xkcoding/http/support/httpclient/HttpClientImpl.java +++ b/src/main/java/com/xkcoding/http/support/httpclient/HttpClientImpl.java @@ -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; @@ -60,6 +62,7 @@ public HttpClientImpl(CloseableHttpClient httpClient) { } private String exec(HttpRequestBase request) { + this.addHeader(request); // 设置超时时长 request.setConfig(RequestConfig.custom() .setConnectTimeout(Constants.TIMEOUT) @@ -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; 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 7a97dd6..b37413b 100644 --- a/src/main/java/com/xkcoding/http/support/java11/HttpClientImpl.java +++ b/src/main/java/com/xkcoding/http/support/java11/HttpClientImpl.java @@ -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; @@ -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 请求 * @@ -102,7 +114,7 @@ public String get(String url, Map params, HttpHeader header, boo MapUtil.forEach(header.getHeaders(), builder::header); } - return exec(builder.build()); + return exec(builder); } /** @@ -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); } /** 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 bf87865..8ca8485 100644 --- a/src/main/java/com/xkcoding/http/support/okhttp3/OkHttp3Impl.java +++ b/src/main/java/com/xkcoding/http/support/okhttp3/OkHttp3Impl.java @@ -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; @@ -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() { @@ -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); @@ -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 请求 * @@ -112,9 +124,9 @@ public String get(String url, Map 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); } /** @@ -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); } /** @@ -200,8 +212,8 @@ public String post(String url, Map 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); } }