Skip to content

Commit

Permalink
Merge pull request #1 from xkcoding/master
Browse files Browse the repository at this point in the history
update fork merge
  • Loading branch information
zhangyd-c authored May 10, 2020
2 parents d8aabc5 + 6dd2a86 commit 1f2e743
Show file tree
Hide file tree
Showing 13 changed files with 249 additions and 82 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ notifications:
cache:
directories:
- '$HOME/.m2/repository'

branches:
only:
- master
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,24 @@
<dependency>
<groupId>com.xkcoding.http</groupId>
<artifactId>simple-http</artifactId>
<version>1.0.2</version>
<version>1.0.1</version>
</dependency>
```

## 特点

- 默认会按照下面的优先级自行寻找底层实现,`java 11 HttpClient -> OkHttp3 -> apache HttpClient -> hutool-http`
- 也可以自行实现 `com.xkcoding.http.support.Http` 接口,通过 `HttpUtil.setHttp(new MyHttpImpl())` 设置进来
- 可以配置超时时间及代理

```java
HttpConfig config = new HttpConfig();
config.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 10080)));
config.setTimeout(15000);
HttpUtil.setConfig(config);
String s = HttpUtil.get("https://www.google.com");
System.out.println("s = " + s);
```

## TODO

Expand Down
29 changes: 13 additions & 16 deletions 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.2</version>
<version>1.0.2-SNAPSHOT</version>

<name>${project.artifactId}</name>
<url>https://github.com/xkcoding/simple-http</url>
Expand All @@ -18,9 +18,9 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<okhttp3.version>4.2.2</okhttp3.version>
<httpclient.version>4.5.10</httpclient.version>
<hutool.version>5.1.0</hutool.version>
<okhttp3.version>4.5.0</okhttp3.version>
<httpclient.version>4.5.12</httpclient.version>
<hutool.version>5.2.5</hutool.version>
</properties>

<developers>
Expand Down Expand Up @@ -208,20 +208,17 @@
<name>aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</repository>
<!--xkcoding 私服-->
<repository>
<id>xkcoding-nexus</id>
<name>xkcoding nexus</name>
<url>https://nexus.xkcoding.com/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
<!--阿里云私服-->
<pluginRepository>
<id>aliyun</id>
<name>aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</pluginRepository>
</pluginRepositories>

<profiles>
<profile>
<id>release</id>
Expand Down
49 changes: 33 additions & 16 deletions src/main/java/com/xkcoding/http/HttpUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

package com.xkcoding.http;

import com.xkcoding.http.config.HttpConfig;
import com.xkcoding.http.exception.SimpleHttpException;
import com.xkcoding.http.support.AbstractHttp;
import com.xkcoding.http.support.Http;
import com.xkcoding.http.support.HttpHeader;
import com.xkcoding.http.support.httpclient.HttpClientImpl;
import com.xkcoding.http.support.hutool.HutoolImpl;
import com.xkcoding.http.support.okhttp3.OkHttp3Impl;
import com.xkcoding.http.util.ClassUtil;
import lombok.experimental.UtilityClass;

Expand All @@ -37,40 +36,58 @@
*/
@UtilityClass
public class HttpUtil {
private static Http proxy;
private static AbstractHttp proxy;

static {
Http defaultProxy = null;
private void selectHttpProxy() {
AbstractHttp defaultProxy = null;
ClassLoader classLoader = HttpUtil.class.getClassLoader();
// 基于 java 11 HttpClient
if (ClassUtil.isPresent("java.net.http.HttpClient", classLoader)) {
defaultProxy = new com.xkcoding.http.support.java11.HttpClientImpl();
defaultProxy = getHttpProxy(com.xkcoding.http.support.java11.HttpClientImpl.class);
}
// 基于 okhttp3
else if (ClassUtil.isPresent("okhttp3.OkHttpClient", classLoader)) {
defaultProxy = new OkHttp3Impl();
if (null == defaultProxy && ClassUtil.isPresent("okhttp3.OkHttpClient", classLoader)) {
defaultProxy = getHttpProxy(com.xkcoding.http.support.okhttp3.OkHttp3Impl.class);
}
// 基于 httpclient
else if (ClassUtil.isPresent("org.apache.http.impl.client.HttpClients", classLoader)) {
defaultProxy = new HttpClientImpl();
if (null == defaultProxy && ClassUtil.isPresent("org.apache.http.impl.client.HttpClients", classLoader)) {
defaultProxy = getHttpProxy(com.xkcoding.http.support.httpclient.HttpClientImpl.class);
}
// 基于 hutool
else if (ClassUtil.isPresent("cn.hutool.http.HttpRequest", classLoader)) {
defaultProxy = new HutoolImpl();
if (null == defaultProxy && ClassUtil.isPresent("cn.hutool.http.HttpRequest", classLoader)) {
defaultProxy = getHttpProxy(com.xkcoding.http.support.hutool.HutoolImpl.class);
}

if (defaultProxy == null) {
throw new SimpleHttpException("Has no HttpImpl defined in environment!");
}

proxy = defaultProxy;
}

public void setHttp(Http http) {
private static <T extends AbstractHttp> AbstractHttp getHttpProxy(Class<T> clazz) {
try {
return clazz.newInstance();
} catch (Throwable e) {
return null;
}
}

public void setHttp(AbstractHttp http) {
proxy = http;
}

private void checkHttpNotNull(Http proxy) {
if (null == proxy) {
throw new SimpleHttpException("HTTP 实现类未指定!");
selectHttpProxy();
}
}

public void setConfig(HttpConfig httpConfig) {
checkHttpNotNull(proxy);
proxy.setHttpConfig(httpConfig);
}

/**
* GET 请求
*
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/xkcoding/http/config/HttpConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.xkcoding.http.config;

import com.xkcoding.http.constants.Constants;
import lombok.Data;

import java.net.Proxy;

/**
* <p>
* Http 配置类
* </p>
*
* @author yangkai.shen
* @date Created in 2020-04-29 14:41
*/
@Data
public class HttpConfig {
/**
* 超时时长,单位毫秒
*/
private int timeout = Constants.DEFAULT_TIMEOUT;
/**
* 代理配置
*/
private Proxy proxy;
}
16 changes: 11 additions & 5 deletions src/main/java/com/xkcoding/http/constants/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public interface Constants {
/**
* 超时时长,单位毫秒
*/
int TIMEOUT = 3000;
int DEFAULT_TIMEOUT = 3000;

/**
* 编码格式
Expand All @@ -54,12 +54,18 @@ public interface Constants {
String CONTENT_ENCODING = "Content-Encoding";

/**
* 空字符串
* User-Agent
*/
String EMPTY = "";
String USER_AGENT = "User-Agent";

/**
* User-Agent
* 模拟 User-Agent
*/
String USER_AGENT_DATA = "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";

/**
* 空字符串
*/
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";
String EMPTY = "";

}
23 changes: 23 additions & 0 deletions src/main/java/com/xkcoding/http/support/AbstractHttp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.xkcoding.http.support;

import com.xkcoding.http.config.HttpConfig;

/**
* <p>
* HTTP 抽象类
* </p>
*
* @author yangkai.shen
* @date Created in 2020-04-29 14:45
*/
public abstract class AbstractHttp implements Http {
protected HttpConfig httpConfig;

public AbstractHttp(HttpConfig httpConfig) {
this.httpConfig = httpConfig;
}

public void setHttpConfig(HttpConfig httpConfig) {
this.httpConfig = httpConfig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@

package com.xkcoding.http.support.httpclient;

import com.xkcoding.http.config.HttpConfig;
import com.xkcoding.http.constants.Constants;
import com.xkcoding.http.exception.SimpleHttpException;
import com.xkcoding.http.support.Http;
import com.xkcoding.http.support.AbstractHttp;
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.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
Expand All @@ -38,6 +39,8 @@
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -50,25 +53,35 @@
* @author yangkai.shen
* @date Created in 2019/12/25 09:24
*/
public class HttpClientImpl implements Http {
public class HttpClientImpl extends AbstractHttp {
private final CloseableHttpClient httpClient;

public HttpClientImpl() {
this(HttpClients.createDefault());
this(HttpClients.createDefault(), new HttpConfig());
}

public HttpClientImpl(CloseableHttpClient httpClient) {
public HttpClientImpl(HttpConfig httpConfig) {
this(HttpClients.createDefault(), httpConfig);
}

public HttpClientImpl(CloseableHttpClient httpClient, HttpConfig httpConfig) {
super(httpConfig);
this.httpClient = httpClient;
}

private String exec(HttpRequestBase request) {
this.addHeader(request);
// 设置超时时长
request.setConfig(RequestConfig.custom()
.setConnectTimeout(Constants.TIMEOUT)
.setSocketTimeout(Constants.TIMEOUT)
.setConnectionRequestTimeout(Constants.TIMEOUT)
.build());
RequestConfig.Builder configBuilder = RequestConfig.custom().setConnectTimeout(httpConfig.getTimeout()).setSocketTimeout(httpConfig.getTimeout()).setConnectionRequestTimeout(httpConfig.getTimeout());
// 设置代理
if (null != httpConfig.getProxy()) {
Proxy proxy = httpConfig.getProxy();
InetSocketAddress address = (InetSocketAddress) proxy.address();
HttpHost host = new HttpHost(address.getHostName(), address.getPort(), proxy.type().name().toLowerCase());
configBuilder.setProxy(host);
}

request.setConfig(configBuilder.build());

try (CloseableHttpResponse response = this.httpClient.execute(request)) {
if (!isSuccess(response)) {
Expand All @@ -92,10 +105,10 @@ private String exec(HttpRequestBase request) {
* @param request HttpRequestBase
*/
private void addHeader(HttpRequestBase request) {
String ua = HttpHeaders.USER_AGENT;
String ua = Constants.USER_AGENT;
Header[] headers = request.getHeaders(ua);
if (null == headers || headers.length == 0) {
request.addHeader(ua, Constants.USER_AGENT);
request.addHeader(ua, Constants.USER_AGENT_DATA);
}
}

Expand Down
22 changes: 18 additions & 4 deletions src/main/java/com/xkcoding/http/support/hutool/HutoolImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.xkcoding.http.constants.Constants;
import com.xkcoding.http.config.HttpConfig;
import com.xkcoding.http.exception.SimpleHttpException;
import com.xkcoding.http.support.Http;
import com.xkcoding.http.support.AbstractHttp;
import com.xkcoding.http.support.HttpHeader;
import com.xkcoding.http.util.MapUtil;
import com.xkcoding.http.util.StringUtil;
Expand All @@ -36,9 +36,23 @@
* @author yangkai.shen
* @date Created in 2019/12/24 19:08
*/
public class HutoolImpl implements Http {
public class HutoolImpl extends AbstractHttp {
public HutoolImpl() {
this(new HttpConfig());
}

public HutoolImpl(HttpConfig httpConfig) {
super(httpConfig);
}

private String exec(HttpRequest request) {
request = request.timeout(Constants.TIMEOUT);
// 设置超时时长
request = request.timeout(httpConfig.getTimeout());
// 设置代理
if (null != httpConfig.getProxy()) {
request = request.setProxy(httpConfig.getProxy());
}

try (HttpResponse response = request.execute()) {
if (!response.isOk()) {
throw new SimpleHttpException("Unexpected code " + response);
Expand Down
Loading

0 comments on commit 1f2e743

Please sign in to comment.