Skip to content

Commit 2c2f670

Browse files
committed
1 采用OkHttp3
1 parent 2905867 commit 2c2f670

9 files changed

Lines changed: 229 additions & 29 deletions

File tree

common/pom.xml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,18 @@
1313

1414
<dependencies>
1515

16-
<dependency>
17-
<groupId>junit</groupId>
18-
<artifactId>junit</artifactId>
19-
<version>4.12</version>
20-
<scope>test</scope>
21-
</dependency>
22-
2316
<dependency>
2417
<groupId>org.slf4j</groupId>
2518
<artifactId>slf4j-api</artifactId>
2619
<version>1.7.9</version>
2720
</dependency>
2821

22+
23+
<dependency>
24+
<groupId>junit</groupId>
25+
<artifactId>junit</artifactId>
26+
</dependency>
27+
2928
<!--util-->
3029
<dependency>
3130
<groupId>com.google.guava</groupId>

http/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
<version>2.0</version>
1919
</dependency>
2020

21+
22+
<dependency>
23+
<groupId>junit</groupId>
24+
<artifactId>junit</artifactId>
25+
</dependency>
26+
2127
<!--http-->
2228
<dependency>
2329
<groupId>io.netty</groupId>
@@ -29,6 +35,12 @@
2935
<artifactId>async-http-client</artifactId>
3036
<version>1.9.40</version>
3137
</dependency>
38+
39+
<dependency>
40+
<groupId>com.squareup.okhttp3</groupId>
41+
<artifactId>okhttp</artifactId>
42+
<version>3.8.1</version>
43+
</dependency>
3244
</dependencies>
3345

3446
</project>

http/src/main/java/pro/tools/http/netty/clientpool/DefaultClientPool.java

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,18 @@
77
import io.netty.channel.pool.ChannelPool;
88
import io.netty.channel.pool.FixedChannelPool;
99
import io.netty.channel.socket.nio.NioSocketChannel;
10-
import io.netty.handler.codec.http.DefaultFullHttpRequest;
11-
import io.netty.handler.codec.http.DefaultHttpHeaders;
12-
import io.netty.handler.codec.http.FullHttpRequest;
13-
import io.netty.handler.codec.http.HttpHeaderNames;
14-
import io.netty.handler.codec.http.HttpHeaderValues;
15-
import io.netty.handler.codec.http.HttpHeaders;
16-
import io.netty.handler.codec.http.HttpVersion;
17-
import io.netty.handler.codec.http.QueryStringEncoder;
10+
import io.netty.handler.codec.http.*;
1811
import io.netty.handler.ssl.SslContext;
1912
import io.netty.handler.ssl.SslContextBuilder;
2013
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
2114
import org.slf4j.Logger;
2215
import org.slf4j.LoggerFactory;
2316
import pro.tools.data.text.ToolJson;
24-
import pro.tools.http.netty.exception.HttpException;
2517
import pro.tools.http.netty.handler.HttpClientChannelPoolHandler;
2618
import pro.tools.http.netty.handler.HttpClientHandler;
2719
import pro.tools.http.netty.handler.HttpClientInitializer;
20+
import pro.tools.http.pojo.*;
2821
import pro.tools.http.pojo.HttpMethod;
29-
import pro.tools.http.pojo.HttpReceive;
30-
import pro.tools.http.pojo.HttpSend;
3122

3223
import javax.net.ssl.SSLException;
3324
import java.net.URI;
@@ -144,13 +135,7 @@ public HttpReceive request(HttpSend httpSend, long timeout, TimeUnit timeUnit) {
144135
static {
145136
defaultHttpHeaders = new DefaultHttpHeaders();
146137

147-
//region DEFAULT
148-
defaultHttpHeaders.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
149-
defaultHttpHeaders.set(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaderValues.GZIP_DEFLATE);
150-
defaultHttpHeaders.set(HttpHeaderNames.USER_AGENT, "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)");
151-
defaultHttpHeaders.set("DNT", 1);
152-
defaultHttpHeaders.set(HttpHeaderNames.CACHE_CONTROL, "max-age=0");
153-
//endregion
138+
HttpDefaultHeaders.getDefaultHeaders().forEach(defaultHttpHeaders::set);
154139
}
155140

156141
public void stop() {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package pro.tools.http.okhttp;
2+
3+
import okhttp3.ConnectionPool;
4+
import okhttp3.OkHttpClient;
5+
6+
import java.util.concurrent.TimeUnit;
7+
8+
/**
9+
* Created on 17/9/3 13:59 星期日.
10+
*
11+
* @author sd
12+
*/
13+
public class ToolHttpBuilder {
14+
15+
/**
16+
* 读超时 秒
17+
*/
18+
private final static long DEFAULT_READ_TIMEOUT = 5;
19+
/**
20+
* 写超时 秒
21+
*/
22+
private final static long DEFAULT_WRITE_TIMEOUT = 5;
23+
/**
24+
* 连接超时 秒
25+
*/
26+
private final static long DEFAULT_CONNECT_TIMEOUT = 5;
27+
/**
28+
* 长连接时间 分钟
29+
*/
30+
private final static long DEFAULT_KEEP_ALIVE_DURATION = 1;
31+
/**
32+
* 最大空闲连接
33+
*/
34+
private final static int DEFAULT_MAX_IDLE_CONNECTIONS = 5;
35+
36+
private static OkHttpClient.Builder defaultBuilder;
37+
38+
static {
39+
init();
40+
}
41+
42+
private static void init() {
43+
defaultBuilder = new OkHttpClient.Builder();
44+
//设置超时时间
45+
defaultBuilder.connectTimeout(DEFAULT_CONNECT_TIMEOUT, TimeUnit.SECONDS);
46+
defaultBuilder.writeTimeout(DEFAULT_WRITE_TIMEOUT, TimeUnit.SECONDS);
47+
defaultBuilder.readTimeout(DEFAULT_READ_TIMEOUT, TimeUnit.SECONDS);
48+
//连接池
49+
ConnectionPool connectionPool = new ConnectionPool(DEFAULT_MAX_IDLE_CONNECTIONS
50+
, DEFAULT_KEEP_ALIVE_DURATION, TimeUnit.MINUTES);
51+
defaultBuilder.connectionPool(connectionPool);
52+
53+
//失败重连
54+
defaultBuilder.retryOnConnectionFailure(true);
55+
}
56+
57+
public synchronized static void setDefaultBuilder(OkHttpClient.Builder defaultBuilder) {
58+
ToolHttpBuilder.defaultBuilder = defaultBuilder;
59+
}
60+
61+
public static OkHttpClient.Builder getDefaultBuilder() {
62+
return defaultBuilder;
63+
}
64+
65+
public static OkHttpClient buildOkHttpClient() {
66+
return defaultBuilder.build();
67+
}
68+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package pro.tools.http.okhttp;
2+
3+
import okhttp3.*;
4+
import pro.tools.data.text.ToolJson;
5+
import pro.tools.http.pojo.HttpDefaultHeaders;
6+
import pro.tools.http.pojo.HttpMethod;
7+
import pro.tools.http.pojo.HttpSend;
8+
9+
import java.io.IOException;
10+
import java.util.AbstractCollection;
11+
import java.util.Map;
12+
13+
/**
14+
* Created on 17/9/3 13:57 星期日.
15+
*
16+
* @author sd
17+
*/
18+
public class ToolSendHttp {
19+
20+
public static void get(HttpSend httpSend) {
21+
OkHttpClient okHttpClient = ToolHttpBuilder.buildOkHttpClient();
22+
Request request = convertRequest(httpSend);
23+
Call call = okHttpClient.newCall(request);
24+
Response response;
25+
try {
26+
response = call.execute();
27+
ResponseBody body = response.body();
28+
System.out.println(body.string());
29+
} catch (IOException e) {
30+
e.printStackTrace();
31+
}
32+
}
33+
34+
private static Request convertRequest(HttpSend httpSend) {
35+
Request.Builder requestBuilder = new Request.Builder();
36+
FormBody.Builder requestBodyBuilder = new FormBody.Builder();
37+
38+
Map<String, Object> params = httpSend.getParams();
39+
40+
if (params != null) {
41+
params.forEach((key, value) -> {
42+
if (value instanceof AbstractCollection
43+
|| value instanceof Map
44+
|| value instanceof Number
45+
|| value instanceof String) {
46+
requestBodyBuilder.addEncoded(key, value.toString());
47+
} else {
48+
requestBodyBuilder.addEncoded(key, ToolJson.anyToJson(value));
49+
}
50+
});
51+
}
52+
System.out.println(httpSend.getMethod().toString());
53+
54+
FormBody build = requestBodyBuilder.build();
55+
56+
if (httpSend.getMethod() != HttpMethod.GET) {
57+
requestBuilder.method(httpSend.getMethod().name(), build);
58+
} else {
59+
requestBuilder.method("POST", build);
60+
}
61+
62+
HttpDefaultHeaders.getDefaultHeaders().forEach((key, value) -> {
63+
if (value instanceof AbstractCollection
64+
|| value instanceof Map
65+
|| value instanceof Number
66+
|| value instanceof String) {
67+
requestBuilder.addHeader(key, value.toString());
68+
} else {
69+
requestBuilder.addHeader(key, ToolJson.anyToJson(value));
70+
}
71+
});
72+
requestBuilder.addHeader("host", httpSend.getUrl());
73+
requestBuilder.addHeader("content-type", "application/x-www-form-urlencoded;charset" + httpSend.getCharset().toString());
74+
75+
return requestBuilder
76+
.url(httpSend.getUrl())
77+
.build();
78+
}
79+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package pro.tools.http.pojo;
2+
3+
import com.google.common.collect.Maps;
4+
5+
import java.util.Map;
6+
7+
/**
8+
* Created on 17/9/3 15:19 星期日.
9+
*
10+
* @author sd
11+
*/
12+
public class HttpDefaultHeaders {
13+
private final static Map<String, Object> defaultHeaders;
14+
15+
static {
16+
defaultHeaders = Maps.newHashMap();
17+
init();
18+
}
19+
20+
private static void init() {
21+
defaultHeaders.put("connection", "keep-alive");
22+
defaultHeaders.put("accept-encoding", "gzip,deflate");
23+
defaultHeaders.put("user-agent", "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)");
24+
defaultHeaders.put("DNT", 1);
25+
defaultHeaders.put("cache-control", "max-age=0");
26+
}
27+
28+
public static Map<String, Object> getDefaultHeaders() {
29+
return defaultHeaders;
30+
}
31+
}

http/src/main/java/pro/tools/http/netty/exception/HttpException.java renamed to http/src/main/java/pro/tools/http/pojo/HttpException.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
package pro.tools.http.netty.exception;
2-
3-
import pro.tools.http.pojo.HttpReceive;
4-
import pro.tools.http.pojo.HttpSend;
1+
package pro.tools.http.pojo;
52

63
/**
74
* @author SeanDragon
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package http;
2+
3+
import org.junit.Test;
4+
import pro.tools.http.okhttp.ToolSendHttp;
5+
import pro.tools.http.pojo.HttpSend;
6+
7+
/**
8+
* Created on 17/9/3 13:12 星期日.
9+
*
10+
* @author sd
11+
*/
12+
public class TestURL {
13+
@Test
14+
public void test1() {
15+
HttpSend httpSend = new HttpSend("https://baidu.com");
16+
ToolSendHttp.get(httpSend);
17+
}
18+
}

pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@
1616
<module>protools</module>
1717
</modules>
1818

19+
<dependencyManagement>
20+
<dependencies>
21+
<dependency>
22+
<groupId>junit</groupId>
23+
<artifactId>junit</artifactId>
24+
<version>4.12</version>
25+
<scope>test</scope>
26+
</dependency>
27+
</dependencies>
28+
</dependencyManagement>
29+
1930
<packaging>pom</packaging>
2031

2132
<organization>

0 commit comments

Comments
 (0)