1616
1717package com .xkcoding .http .support .httpclient ;
1818
19+ import com .xkcoding .http .config .HttpConfig ;
1920import com .xkcoding .http .constants .Constants ;
2021import com .xkcoding .http .exception .SimpleHttpException ;
21- import com .xkcoding .http .support .Http ;
22+ import com .xkcoding .http .support .AbstractHttp ;
2223import com .xkcoding .http .support .HttpHeader ;
2324import com .xkcoding .http .util .MapUtil ;
2425import com .xkcoding .http .util .StringUtil ;
2526import org .apache .http .Header ;
26- import org .apache .http .HttpHeaders ;
27+ import org .apache .http .HttpHost ;
2728import org .apache .http .NameValuePair ;
2829import org .apache .http .client .config .RequestConfig ;
2930import org .apache .http .client .entity .UrlEncodedFormEntity ;
3839import org .apache .http .util .EntityUtils ;
3940
4041import java .io .IOException ;
42+ import java .net .InetSocketAddress ;
43+ import java .net .Proxy ;
4144import java .util .ArrayList ;
4245import java .util .List ;
4346import java .util .Map ;
5053 * @author yangkai.shen
5154 * @date Created in 2019/12/25 09:24
5255 */
53- public class HttpClientImpl implements Http {
56+ public class HttpClientImpl extends AbstractHttp {
5457 private final CloseableHttpClient httpClient ;
5558
5659 public HttpClientImpl () {
57- this (HttpClients .createDefault ());
60+ this (HttpClients .createDefault (), new HttpConfig () );
5861 }
5962
60- public HttpClientImpl (CloseableHttpClient httpClient ) {
63+ public HttpClientImpl (HttpConfig httpConfig ) {
64+ this (HttpClients .createDefault (), httpConfig );
65+ }
66+
67+ public HttpClientImpl (CloseableHttpClient httpClient , HttpConfig httpConfig ) {
68+ super (httpConfig );
6169 this .httpClient = httpClient ;
6270 }
6371
6472 private String exec (HttpRequestBase request ) {
6573 this .addHeader (request );
6674 // 设置超时时长
67- request .setConfig (RequestConfig .custom ()
68- .setConnectTimeout (Constants .TIMEOUT )
69- .setSocketTimeout (Constants .TIMEOUT )
70- .setConnectionRequestTimeout (Constants .TIMEOUT )
71- .build ());
75+ RequestConfig .Builder configBuilder = RequestConfig .custom ().setConnectTimeout (httpConfig .getTimeout ()).setSocketTimeout (httpConfig .getTimeout ()).setConnectionRequestTimeout (httpConfig .getTimeout ());
76+ // 设置代理
77+ if (null != httpConfig .getProxy ()) {
78+ Proxy proxy = httpConfig .getProxy ();
79+ InetSocketAddress address = (InetSocketAddress ) proxy .address ();
80+ HttpHost host = new HttpHost (address .getHostName (), address .getPort (), proxy .type ().name ().toLowerCase ());
81+ configBuilder .setProxy (host );
82+ }
83+
84+ request .setConfig (configBuilder .build ());
7285
7386 try (CloseableHttpResponse response = this .httpClient .execute (request )) {
7487 if (!isSuccess (response )) {
@@ -92,10 +105,10 @@ private String exec(HttpRequestBase request) {
92105 * @param request HttpRequestBase
93106 */
94107 private void addHeader (HttpRequestBase request ) {
95- String ua = HttpHeaders .USER_AGENT ;
108+ String ua = Constants .USER_AGENT ;
96109 Header [] headers = request .getHeaders (ua );
97110 if (null == headers || headers .length == 0 ) {
98- request .addHeader (ua , Constants .USER_AGENT );
111+ request .addHeader (ua , Constants .USER_AGENT_DATA );
99112 }
100113 }
101114
0 commit comments