Skip to content

Commit 73fce5b

Browse files
author
崔成龙
committed
1.HCB、HACB中添加设置ssl版本号的方法;
2.优化HCB中的ssl方法; 3.优化HttpClientUtil、HttpAsyncClientUtil的head请求方法,返回值为http版本号和状态码 4.在HttpClientUtil中添加status方法,返回statusCode值(int) 5.添加Demo类,里面是工具类简要的使用说明 6.修改一些与代码不对应的注释
1 parent 3f52626 commit 73fce5b

8 files changed

Lines changed: 302 additions & 88 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.tgb.ccl.http</groupId>
55
<artifactId>httpclientUtil</artifactId>
6-
<version>2.2.0-beta</version>
6+
<version>2.3.0-beta</version>
77
<description>基于HttpClient-4.4.1、HttpAsyncClient-4.1封装的工具类。支持插件式配置Header、插件式配置httpclient对象,这样就可以方便地自定义header信息、配置ssl、配置proxy、Cookie等</description>
88

99
<properties>

src/main/java/com/tgb/ccl/http/common/SSLs.java

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ public static HostnameVerifier getVerifier() {
7777
return simpleVerifier;
7878
}
7979

80-
public synchronized SSLSocketFactory getSSLSF() throws HttpProcessException {
80+
public synchronized SSLSocketFactory getSSLSF(SSLProtocolVersion sslpv) throws HttpProcessException {
8181
if (sslFactory != null)
8282
return sslFactory;
8383
try {
84-
SSLContext sc = getSSLContext();
84+
SSLContext sc = getSSLContext(sslpv);
8585
sc.init(null, new TrustManager[] { simpleVerifier }, null);
8686
sslFactory = sc.getSocketFactory();
8787
} catch (KeyManagementException e) {
@@ -90,11 +90,11 @@ public synchronized SSLSocketFactory getSSLSF() throws HttpProcessException {
9090
return sslFactory;
9191
}
9292

93-
public synchronized SSLConnectionSocketFactory getSSLCONNSF() throws HttpProcessException {
93+
public synchronized SSLConnectionSocketFactory getSSLCONNSF(SSLProtocolVersion sslpv) throws HttpProcessException {
9494
if (sslConnFactory != null)
9595
return sslConnFactory;
9696
try {
97-
SSLContext sc = getSSLContext();
97+
SSLContext sc = getSSLContext(sslpv);
9898
// sc.init(null, new TrustManager[] { simpleVerifier }, null);
9999
sc.init(null, new TrustManager[] { simpleVerifier }, new java.security.SecureRandom());
100100
sslConnFactory = new SSLConnectionSocketFactory(sc, simpleVerifier);
@@ -104,11 +104,11 @@ public synchronized SSLConnectionSocketFactory getSSLCONNSF() throws HttpProcess
104104
return sslConnFactory;
105105
}
106106

107-
public synchronized SSLIOSessionStrategy getSSLIOSS() throws HttpProcessException {
107+
public synchronized SSLIOSessionStrategy getSSLIOSS(SSLProtocolVersion sslpv) throws HttpProcessException {
108108
if (sslIOSessionStrategy != null)
109109
return sslIOSessionStrategy;
110110
try {
111-
SSLContext sc = getSSLContext();
111+
SSLContext sc = getSSLContext(sslpv);
112112
// sc.init(null, new TrustManager[] { simpleVerifier }, null);
113113
sc.init(null, new TrustManager[] { simpleVerifier }, new java.security.SecureRandom());
114114
sslIOSessionStrategy = new SSLIOSessionStrategy(sc, simpleVerifier);
@@ -147,14 +147,46 @@ public SSLs customSSL(String keyStorePath, String keyStorepass) throws HttpProce
147147
return this;
148148
}
149149

150-
public SSLContext getSSLContext() throws HttpProcessException{
150+
public SSLContext getSSLContext(SSLProtocolVersion sslpv) throws HttpProcessException{
151151
try {
152152
if(sc==null){
153-
sc = SSLContext.getInstance("SSLv3");
153+
sc = SSLContext.getInstance(sslpv.getName());
154154
}
155-
return sc;
156-
} catch (NoSuchAlgorithmException e) {
157-
throw new HttpProcessException(e);
158-
}
155+
return sc;
156+
} catch (NoSuchAlgorithmException e) {
157+
throw new HttpProcessException(e);
158+
}
159+
}
160+
161+
/**
162+
* The SSL protocol version (SSLv3, TLSv1, TLSv1.1, TLSv1.2)
163+
*
164+
* @author arron
165+
* @date 2016年11月18日 上午9:35:37
166+
* @version 1.0
167+
*/
168+
public static enum SSLProtocolVersion{
169+
SSL("SSL"),
170+
SSLv3("SSLv3"),
171+
TLSv1("TLSv1"),
172+
TLSv1_1("TLSv1.1"),
173+
TLSv1_2("TLSv1.2"),
174+
;
175+
private String name;
176+
private SSLProtocolVersion(String name){
177+
this.name = name;
178+
}
179+
public String getName(){
180+
return this.name;
181+
}
182+
public static SSLProtocolVersion find(String name){
183+
for (SSLProtocolVersion pv : SSLProtocolVersion.values()) {
184+
if(pv.getName().toUpperCase().equals(name.toUpperCase())){
185+
return pv;
186+
}
187+
}
188+
throw new RuntimeException("未支持当前ssl版本号:"+name);
189+
}
190+
159191
}
160192
}

src/main/java/com/tgb/ccl/http/httpclient/HttpAsyncClientUtil.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ public static void delete(HttpConfig config) throws HttpProcessException {
184184
*
185185
* @param client client对象
186186
* @param url 资源地址
187+
* @param parasMap 请求参数
187188
* @param headers 请求头信息
188189
* @param context http上下文,用于cookie操作
189190
* @param encoding 编码
@@ -479,7 +480,7 @@ private static HttpRequestBase getRequest(String url, HttpMethods method) {
479480
/**
480481
* 转化为字符串
481482
*
482-
* @param entity 实体
483+
* @param resp 响应对象
483484
* @param encoding 编码
484485
* @return
485486
* @throws HttpProcessException
@@ -501,9 +502,11 @@ private static String fmt2String(HttpResponse resp, String encoding) throws Http
501502
body = sb.toString();
502503
} finally {
503504
instream.close();
504-
EntityUtils.consume(entity);
505505
}
506+
}else{
507+
body = resp.getStatusLine().toString();
506508
}
509+
EntityUtils.consume(entity);
507510
} catch (UnsupportedOperationException e) {
508511
Utils.exception(e);
509512
} catch (IOException e) {
@@ -515,7 +518,7 @@ private static String fmt2String(HttpResponse resp, String encoding) throws Http
515518
/**
516519
* 转化为流
517520
*
518-
* @param entity 实体
521+
* @param resp 响应对象
519522
* @param out 输出流
520523
* @return
521524
* @throws HttpProcessException

src/main/java/com/tgb/ccl/http/httpclient/HttpClientUtil.java

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public class HttpClientUtil{
6060
* 如果已开启连接池,则自动调用build方法,从连接池中获取client对象<br>
6161
* 否则,直接返回相应的默认client对象<br>
6262
*
63+
* @param config 请求参数配置
6364
* @throws HttpProcessException
6465
*/
6566
private static void create(HttpConfig config) throws HttpProcessException {
@@ -114,8 +115,8 @@ public static String get(HttpConfig config) throws HttpProcessException {
114115
*
115116
* @param client client对象
116117
* @param url 资源地址
117-
* @param parasMap 请求参数
118118
* @param headers 请求头信息
119+
* @param parasMap 请求参数
119120
* @param context http上下文,用于cookie操作
120121
* @param encoding 编码
121122
* @return 返回处理结果
@@ -306,7 +307,6 @@ public static OutputStream down(HttpClient client, String url, Header[] headers,
306307
* 下载文件
307308
*
308309
* @param config 请求参数配置
309-
* @param out 输出流
310310
* @return 返回处理结果
311311
* @throws HttpProcessException
312312
*/
@@ -341,6 +341,31 @@ public static String upload(HttpConfig config) throws HttpProcessException {
341341
}
342342
return send(config);
343343
}
344+
345+
/**
346+
* 查看资源链接情况,返回状态码
347+
*
348+
* @param client client对象
349+
* @param url 资源地址
350+
* @param headers 请求头信息
351+
* @param context http上下文,用于cookie操作
352+
* @return 返回处理结果
353+
* @throws HttpProcessException
354+
*/
355+
public static int status(HttpClient client, String url, Header[] headers, HttpContext context, HttpMethods method) throws HttpProcessException {
356+
return status(HttpConfig.custom().client(client).url(url).headers(headers).context(context).method(method));
357+
}
358+
359+
/**
360+
* 查看资源链接情况,返回状态码
361+
*
362+
* @param config 请求参数配置
363+
* @return 返回处理结果
364+
* @throws HttpProcessException
365+
*/
366+
public static int status(HttpConfig config) throws HttpProcessException {
367+
return fmt2Int(execute(config));
368+
}
344369

345370
//-----------华----丽----分----割----线--------------
346371
//-----------华----丽----分----割----线--------------
@@ -349,7 +374,7 @@ public static String upload(HttpConfig config) throws HttpProcessException {
349374
/**
350375
* 请求资源或服务
351376
*
352-
* @param config
377+
* @param config 请求参数配置
353378
* @return
354379
* @throws HttpProcessException
355380
*/
@@ -360,13 +385,8 @@ public static String send(HttpConfig config) throws HttpProcessException {
360385
/**
361386
* 请求资源或服务
362387
*
363-
* @param client client对象
364-
* @param url 资源地址
365-
* @param httpMethod 请求方法
366-
* @param parasMap 请求参数
367-
* @param headers 请求头信息
368-
* @param encoding 编码
369-
* @return 返回处理结果
388+
* @param config 请求参数配置
389+
* @return 返回HttpResponse对象
370390
* @throws HttpProcessException
371391
*/
372392
private static HttpResponse execute(HttpConfig config) throws HttpProcessException {
@@ -426,7 +446,7 @@ private static HttpResponse execute(HttpConfig config) throws HttpProcessExcepti
426446
/**
427447
* 转化为字符串
428448
*
429-
* @param entity 实体
449+
* @param resp 响应对象
430450
* @param encoding 编码
431451
* @return
432452
* @throws HttpProcessException
@@ -438,6 +458,8 @@ private static String fmt2String(HttpResponse resp, String encoding) throws Http
438458
// 按指定编码转换结果实体为String类型
439459
body = EntityUtils.toString(resp.getEntity(), encoding);
440460
Utils.info(body);
461+
}else{//有可能是head请求
462+
body =resp.getStatusLine().toString();
441463
}
442464
EntityUtils.consume(resp.getEntity());
443465
} catch (IOException e) {
@@ -448,10 +470,30 @@ private static String fmt2String(HttpResponse resp, String encoding) throws Http
448470
return body;
449471
}
450472

473+
/**
474+
* 转化为数字
475+
*
476+
* @param resp 响应对象
477+
* @return
478+
* @throws HttpProcessException
479+
*/
480+
private static int fmt2Int(HttpResponse resp) throws HttpProcessException {
481+
int statusCode;
482+
try {
483+
statusCode = resp.getStatusLine().getStatusCode();
484+
EntityUtils.consume(resp.getEntity());
485+
} catch (IOException e) {
486+
throw new HttpProcessException(e);
487+
}finally{
488+
close(resp);
489+
}
490+
return statusCode;
491+
}
492+
451493
/**
452494
* 转化为流
453495
*
454-
* @param entity 实体
496+
* @param resp 响应对象
455497
* @param out 输出流
456498
* @return
457499
* @throws HttpProcessException

src/main/java/com/tgb/ccl/http/httpclient/builder/HACB.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.apache.http.nio.reactor.IOReactorException;
1616

1717
import com.tgb.ccl.http.common.SSLs;
18+
import com.tgb.ccl.http.common.SSLs.SSLProtocolVersion;
1819
import com.tgb.ccl.http.exception.HttpProcessException;
1920

2021
/**
@@ -28,6 +29,7 @@ public class HACB extends HttpAsyncClientBuilder{
2829

2930
public boolean isSetPool=false;//记录是否设置了连接池
3031
private boolean isNewSSL=false;//记录是否设置了更新了ssl
32+
private SSLProtocolVersion sslpv=SSLProtocolVersion.SSLv3;//ssl 协议版本
3133

3234
//用于配置ssl
3335
private SSLs ssls = SSLs.getInstance();
@@ -70,7 +72,7 @@ public HACB ssl() throws HttpProcessException {
7072
// protocol schemes.
7173
Registry<SchemeIOSessionStrategy> sessionStrategyRegistry = RegistryBuilder.<SchemeIOSessionStrategy>create()
7274
.register("http", NoopIOSessionStrategy.INSTANCE)
73-
.register("https", ssls.getSSLIOSS())
75+
.register("https", ssls.getSSLIOSS(sslpv))
7476
.build();
7577
//配置io线程
7678
IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setIoThreadCount(Runtime.getRuntime().availableProcessors()).build();
@@ -127,7 +129,7 @@ public HACB pool(int maxTotal, int defaultMaxPerRoute) throws HttpProcessExcepti
127129
// protocol schemes.
128130
Registry<SchemeIOSessionStrategy> sessionStrategyRegistry = RegistryBuilder.<SchemeIOSessionStrategy>create()
129131
.register("http", NoopIOSessionStrategy.INSTANCE)
130-
.register("https", ssls.getSSLIOSS())
132+
.register("https", ssls.getSSLIOSS(sslpv))
131133
.build();
132134
//配置io线程
133135
IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setIoThreadCount(12).build();
@@ -158,4 +160,27 @@ public HACB proxy(String hostOrIP, int port){
158160
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
159161
return (HACB) this.setRoutePlanner(routePlanner);
160162
}
163+
164+
/**
165+
* 设置ssl版本<br>
166+
* 如果您想要设置ssl版本,必须<b>先调用此方法,再调用ssl方法<br>
167+
* 仅支持 SSLv3,TSLv1,TSLv1.1,TSLv1.2</b>
168+
* @param sslpv
169+
* @return
170+
*/
171+
public HACB sslpv(String sslpv){
172+
return sslpv(SSLProtocolVersion.find(sslpv));
173+
}
174+
175+
/**
176+
* 设置ssl版本<br>
177+
* 如果您想要设置ssl版本,必须<b>先调用此方法,再调用ssl方法<br>
178+
* 仅支持 SSLv3,TSLv1,TSLv1.1,TSLv1.2</b>
179+
* @param sslpv
180+
* @return
181+
*/
182+
public HACB sslpv(SSLProtocolVersion sslpv){
183+
this.sslpv = sslpv;
184+
return this;
185+
}
161186
}

0 commit comments

Comments
 (0)