1
1
package org .javawebstack .httpclient .implementation ;
2
2
3
- import org .apache .http .Header ;
4
- import org .apache .http .HttpEntity ;
5
- import org .apache .http .HttpResponse ;
6
- import org .apache .http .client .HttpClient ;
7
- import org .apache .http .client .config .RequestConfig ;
8
- import org .apache .http .client .methods .RequestBuilder ;
9
- import org .apache .http .conn .ssl .NoopHostnameVerifier ;
10
- import org .apache .http .conn .ssl .TrustAllStrategy ;
11
- import org .apache .http .entity .ByteArrayEntity ;
12
- import org .apache .http .entity .ContentType ;
13
- import org .apache .http .impl .client .HttpClientBuilder ;
14
- import org .apache .http .ssl .SSLContextBuilder ;
15
-
3
+ import org .apache .hc .client5 .http .classic .HttpClient ;
4
+ import org .apache .hc .client5 .http .config .RequestConfig ;
5
+ import org .apache .hc .client5 .http .impl .classic .HttpClientBuilder ;
6
+ import org .apache .hc .client5 .http .impl .io .PoolingHttpClientConnectionManagerBuilder ;
7
+ import org .apache .hc .client5 .http .io .HttpClientConnectionManager ;
8
+ import org .apache .hc .client5 .http .ssl .NoopHostnameVerifier ;
9
+ import org .apache .hc .client5 .http .ssl .SSLConnectionSocketFactoryBuilder ;
10
+ import org .apache .hc .client5 .http .ssl .TrustAllStrategy ;
11
+ import org .apache .hc .core5 .http .*;
12
+ import org .apache .hc .core5 .http .io .entity .ByteArrayEntity ;
13
+ import org .apache .hc .core5 .http .io .support .ClassicRequestBuilder ;
14
+ import org .apache .hc .core5 .ssl .SSLContextBuilder ;
15
+
16
+ import javax .net .ssl .SSLContext ;
17
+ import java .io .ByteArrayInputStream ;
16
18
import java .io .IOException ;
17
19
import java .io .InputStream ;
18
20
import java .security .KeyManagementException ;
19
21
import java .security .KeyStoreException ;
20
22
import java .security .NoSuchAlgorithmException ;
21
23
import java .util .*;
24
+ import java .util .concurrent .TimeUnit ;
22
25
23
26
public class ApacheHTTPRequestImplementation implements IHTTPRequestImplementation {
24
27
@@ -76,6 +79,8 @@ public Map<String, String[]> getResponseHeaders() {
76
79
}
77
80
78
81
public InputStream getResponseStream () {
82
+ if (responseEntity == null )
83
+ return new ByteArrayInputStream (new byte [0 ]);
79
84
try {
80
85
return responseEntity .getContent ();
81
86
} catch (IOException ignored ) {
@@ -86,22 +91,28 @@ public InputStream getResponseStream() {
86
91
public int execute () {
87
92
try {
88
93
RequestConfig config = RequestConfig .custom ()
89
- .setConnectTimeout (timeout )
90
- .setConnectionRequestTimeout (timeout )
94
+ .setConnectTimeout (timeout , TimeUnit . MILLISECONDS )
95
+ .setConnectionRequestTimeout (timeout , TimeUnit . MILLISECONDS )
91
96
.setRedirectsEnabled (followRedirects )
92
97
.build ();
93
98
HttpClientBuilder clientBuilder = HttpClientBuilder .create ()
94
99
.setDefaultRequestConfig (config );
95
100
96
101
if (!sslVerification ) {
97
- clientBuilder
98
- .setSSLContext (new SSLContextBuilder ().loadTrustMaterial (null , TrustAllStrategy .INSTANCE ).build ())
99
- .setSSLHostnameVerifier (NoopHostnameVerifier .INSTANCE );
102
+ SSLContext context = new SSLContextBuilder ().loadTrustMaterial (null , TrustAllStrategy .INSTANCE ).build ();
103
+ HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder .create ()
104
+ .setSSLSocketFactory (SSLConnectionSocketFactoryBuilder .create ()
105
+ .setSslContext (context )
106
+ .setHostnameVerifier (NoopHostnameVerifier .INSTANCE )
107
+ .build ()
108
+ )
109
+ .build ();
110
+ clientBuilder .setConnectionManager (cm );
100
111
}
101
112
102
113
HttpClient client = clientBuilder .build ();
103
114
104
- RequestBuilder builder = RequestBuilder .create (method );
115
+ ClassicRequestBuilder builder = ClassicRequestBuilder .create (method );
105
116
builder .setUri (url );
106
117
requestHeaders .forEach ((k , values ) -> {
107
118
for (String v : values )
@@ -113,12 +124,13 @@ public int execute() {
113
124
builder .setEntity (new ByteArrayEntity (requestBody , ContentType .create (contentType )));
114
125
}
115
126
116
- HttpResponse response = client .execute (builder .build ());
127
+ ClassicHttpResponse response = client .execute (builder .build (), res -> res );
128
+ responseEntity = response .getEntity ();
117
129
118
- status = response .getStatusLine (). getStatusCode ();
119
- statusMessage = response .getStatusLine (). getReasonPhrase ();
130
+ status = response .getCode ();
131
+ statusMessage = response .getReasonPhrase ();
120
132
Map <String , List <String >> resHeaders = new HashMap <>();
121
- for (Header h : response .getAllHeaders ())
133
+ for (Header h : response .getHeaders ())
122
134
resHeaders .computeIfAbsent (h .getName ().toLowerCase (Locale .ROOT ), n -> new ArrayList <>()).add (h .getValue ());
123
135
resHeaders .forEach ((k , v ) -> responseHeaders .put (k , v .toArray (new String [0 ])));
124
136
0 commit comments