1
1
package com .unidev .httpclient .apache ;
2
2
3
- import com .unidev .platform .Randoms ;
4
- import com .unidev .platform .Strings ;
5
3
import com .unidev .httpclient .socks .PlainSocksSocketFactory ;
6
4
import com .unidev .httpclient .socks .SSLSocksSocketFactory ;
7
- import java .io .IOException ;
8
- import java .util .List ;
9
- import java .util .concurrent .TimeUnit ;
5
+ import com .unidev .platform .Randoms ;
6
+ import com .unidev .platform .Strings ;
10
7
import org .apache .commons .collections4 .CollectionUtils ;
11
- import org .apache .http .Header ;
12
- import org .apache .http .HttpEntity ;
13
- import org .apache .http .HttpHost ;
14
- import org .apache .http .HttpResponse ;
15
- import org .apache .http .StatusLine ;
8
+ import org .apache .http .*;
16
9
import org .apache .http .client .ClientProtocolException ;
17
10
import org .apache .http .client .HttpClient ;
18
11
import org .apache .http .client .config .RequestConfig ;
19
- import org .apache .http .client .methods .HttpDelete ;
20
- import org .apache .http .client .methods .HttpGet ;
21
- import org .apache .http .client .methods .HttpHead ;
22
- import org .apache .http .client .methods .HttpPatch ;
23
- import org .apache .http .client .methods .HttpPost ;
24
- import org .apache .http .client .methods .HttpPut ;
25
- import org .apache .http .client .methods .HttpUriRequest ;
12
+ import org .apache .http .client .methods .*;
26
13
import org .apache .http .config .RegistryBuilder ;
27
14
import org .apache .http .conn .socket .ConnectionSocketFactory ;
28
15
import org .apache .http .cookie .Cookie ;
36
23
import org .apache .http .protocol .HttpContext ;
37
24
import org .apache .http .util .EntityUtils ;
38
25
26
+ import java .io .IOException ;
27
+ import java .util .List ;
28
+ import java .util .concurrent .TimeUnit ;
29
+
39
30
/**
40
31
* Basic HTTP client, used to get stuff from web pages
41
32
*/
@@ -111,9 +102,9 @@ public void init(String socksIP, int socksPort) {
111
102
*/
112
103
protected RequestConfig .Builder buildRequestConfig () {
113
104
return RequestConfig .custom ().setCircularRedirectsAllowed (true )
114
- .setConnectTimeout (connectionTimeout )
115
- .setConnectionRequestTimeout (connectionTimeout )
116
- .setSocketTimeout (connectionTimeout );
105
+ .setConnectTimeout (connectionTimeout )
106
+ .setConnectionRequestTimeout (connectionTimeout )
107
+ .setSocketTimeout (connectionTimeout );
117
108
}
118
109
119
110
/**
@@ -142,11 +133,11 @@ public String get(String url, Header... headers) throws IOException {
142
133
rsp = httpClient .execute (httpget , context );
143
134
144
135
HttpUriRequest currentReq = (HttpUriRequest ) context .getAttribute (
145
- ExecutionContext .HTTP_REQUEST );
136
+ ExecutionContext .HTTP_REQUEST );
146
137
HttpHost currentHost = (HttpHost ) context .getAttribute (
147
- ExecutionContext .HTTP_TARGET_HOST );
138
+ ExecutionContext .HTTP_TARGET_HOST );
148
139
currentUrl = (currentReq .getURI ().isAbsolute ()) ? currentReq .getURI ().toString ()
149
- : (currentHost .toURI () + currentReq .getURI ());
140
+ : (currentHost .toURI () + currentReq .getURI ());
150
141
151
142
HttpEntity entity = rsp .getEntity ();
152
143
lastResponseHeaders = rsp .getAllHeaders ();
@@ -159,7 +150,7 @@ public String get(String url, Header... headers) throws IOException {
159
150
}
160
151
161
152
public HttpEntity getAsEntity (String url , Header ... headers )
162
- throws ClientProtocolException , IOException {
153
+ throws ClientProtocolException , IOException {
163
154
HttpGet httpget = new HttpGet (url );
164
155
copyHeaders (httpget , headers );
165
156
rsp = httpClient .execute (httpget , context );
@@ -199,11 +190,11 @@ public String post(String url, HttpEntity data, Header... headers) throws IOExce
199
190
rsp = httpClient .execute (httpPost , context );
200
191
201
192
HttpUriRequest currentReq = (HttpUriRequest ) context .getAttribute (
202
- ExecutionContext .HTTP_REQUEST );
193
+ ExecutionContext .HTTP_REQUEST );
203
194
HttpHost currentHost = (HttpHost ) context .getAttribute (
204
- ExecutionContext .HTTP_TARGET_HOST );
195
+ ExecutionContext .HTTP_TARGET_HOST );
205
196
currentUrl = (currentReq .getURI ().isAbsolute ()) ? currentReq .getURI ().toString ()
206
- : (currentHost .toURI () + currentReq .getURI ());
197
+ : (currentHost .toURI () + currentReq .getURI ());
207
198
208
199
HttpEntity entity = rsp .getEntity ();
209
200
@@ -217,7 +208,7 @@ public String post(String url, HttpEntity data, Header... headers) throws IOExce
217
208
}
218
209
219
210
public HttpEntity postAsEntity (String url , Header ... headers )
220
- throws ClientProtocolException , IOException {
211
+ throws ClientProtocolException , IOException {
221
212
HttpPost httpPost = new HttpPost (url );
222
213
copyHeaders (httpPost , headers );
223
214
@@ -311,10 +302,10 @@ public Cookie getCookie(String name) {
311
302
312
303
private void createProxyConnectionManager (String socksIP , int socksPort ) {
313
304
connectionManager = new PoolingHttpClientConnectionManager (
314
- RegistryBuilder .<ConnectionSocketFactory >create ()
315
- .register ("http" , new PlainSocksSocketFactory (socksIP , socksPort ))
316
- .register ("https" , new SSLSocksSocketFactory (socksIP , socksPort ))
317
- .build ()
305
+ RegistryBuilder .<ConnectionSocketFactory >create ()
306
+ .register ("http" , new PlainSocksSocketFactory (socksIP , socksPort ))
307
+ .register ("https" , new SSLSocksSocketFactory (socksIP , socksPort ))
308
+ .build ()
318
309
);
319
310
}
320
311
@@ -403,7 +394,7 @@ public PoolingHttpClientConnectionManager getConnectionManager() {
403
394
}
404
395
405
396
public void setConnectionManager (
406
- PoolingHttpClientConnectionManager connectionManager ) {
397
+ PoolingHttpClientConnectionManager connectionManager ) {
407
398
this .connectionManager = connectionManager ;
408
399
}
409
400
}
0 commit comments