26
26
import org .apache .http .impl .client .BasicCookieStore ;
27
27
import org .apache .http .impl .client .BasicCredentialsProvider ;
28
28
import org .apache .http .impl .client .HttpClientBuilder ;
29
+ import org .apache .http .impl .client .LaxRedirectStrategy ;
29
30
import org .apache .http .ssl .SSLContextBuilder ;
31
+ import org .jetbrains .annotations .NotNull ;
30
32
import org .json .JSONObject ;
31
33
32
34
import javax .net .ssl .SSLContext ;
@@ -94,6 +96,13 @@ public HttpClient newHttpClient() throws NoSuchAlgorithmException, KeyManagement
94
96
HttpClientBuilder httpClientBuilder = HttpClientBuilder .create ();
95
97
httpClientBuilder .setUserAgent (userAgent );
96
98
99
+ /*
100
+ * This line below allow redirection (301, 302, 303, 307, 308) to be followed automatically
101
+ * in the case of a POST request. By default, the HttpClient does not follow redirections for POST requests.
102
+ * This is specified in the HTTP specification. (HTTP RFC 2616)
103
+ */
104
+ httpClientBuilder .setRedirectStrategy (new LaxRedirectStrategy ());
105
+
97
106
/*
98
107
* Create a SSLContext that uses our Trust Strategy to trust all self-signed certificates.
99
108
*/
@@ -124,12 +133,22 @@ public HttpClient newHttpClient() throws NoSuchAlgorithmException, KeyManagement
124
133
return (httpClientBuilder .build ());
125
134
}
126
135
127
- public RequestResponse sendGet (String url ) throws IOException , HttpClientException , HttpServerException {
136
+ /*
137
+ $ GET
138
+ */
139
+
140
+ public @ NotNull RequestResponse sendGet (@ NotNull String url ) throws IOException , HttpClientException , HttpServerException {
141
+ return (sendGet (url , getRequestConfig ().build ()));
142
+ }
143
+
144
+ public @ NotNull RequestResponse sendGet (@ NotNull String url , @ NotNull RequestConfig requestConfig ) throws IOException ,
145
+ HttpClientException ,
146
+ HttpServerException {
128
147
Date start = new Date ();
129
148
String oldCookieStoreSerialized = BasicCookieStoreSerializerUtils .serializableToBase64 (httpCookieStore );
130
149
131
150
HttpGet httpGet = new HttpGet (url );
132
- httpGet .setConfig (getRequestConfig (). build () );
151
+ httpGet .setConfig (requestConfig );
133
152
httpGet .addHeader (HttpHeaders .ACCEPT , "application/json, text/plain, */*" );
134
153
HttpResponse httpResponse = getHttpClient ().execute (httpGet );
135
154
@@ -138,6 +157,10 @@ public RequestResponse sendGet(String url) throws IOException, HttpClientExcepti
138
157
return (new RequestResponse (httpResponse , start ));
139
158
}
140
159
160
+ /*
161
+ $ POST
162
+ */
163
+
141
164
public RequestResponse sendPost (String url , String content , ContentType contentType ) throws IOException , HttpClientException , HttpServerException {
142
165
Date start = new Date ();
143
166
String oldCookieStoreSerialized = BasicCookieStoreSerializerUtils .serializableToBase64 (httpCookieStore );
@@ -154,12 +177,27 @@ public RequestResponse sendPost(String url, String content, ContentType contentT
154
177
return (new RequestResponse (httpResponse , start ));
155
178
}
156
179
157
- public RequestResponse sendForm (String url , List <NameValuePair > form ) throws IOException , HttpClientException , HttpServerException {
180
+ /*
181
+ $ FORM
182
+ */
183
+
184
+ public @ NotNull RequestResponse sendForm (@ NotNull String url ,
185
+ @ NotNull List <NameValuePair > form ) throws IOException ,
186
+ HttpClientException ,
187
+ HttpServerException {
188
+ return (sendForm (url , form , getRequestConfig ().build ()));
189
+ }
190
+
191
+ public @ NotNull RequestResponse sendForm (@ NotNull String url ,
192
+ @ NotNull List <NameValuePair > form ,
193
+ @ NotNull RequestConfig requestConfig ) throws IOException ,
194
+ HttpClientException ,
195
+ HttpServerException {
158
196
Date start = new Date ();
159
197
String oldCookieStoreSerialized = BasicCookieStoreSerializerUtils .serializableToBase64 (httpCookieStore );
160
198
161
199
HttpPost httpPost = new HttpPost (url );
162
- httpPost .setConfig (getRequestConfig (). build () );
200
+ httpPost .setConfig (requestConfig );
163
201
httpPost .addHeader (HttpHeaders .ACCEPT , "application/json, text/plain, */*" );
164
202
UrlEncodedFormEntity entity = new UrlEncodedFormEntity (form , Consts .UTF_8 );
165
203
httpPost .setEntity (entity );
0 commit comments