2626import org .apache .http .impl .client .BasicCookieStore ;
2727import org .apache .http .impl .client .BasicCredentialsProvider ;
2828import org .apache .http .impl .client .HttpClientBuilder ;
29+ import org .apache .http .impl .client .LaxRedirectStrategy ;
2930import org .apache .http .ssl .SSLContextBuilder ;
31+ import org .jetbrains .annotations .NotNull ;
3032import org .json .JSONObject ;
3133
3234import javax .net .ssl .SSLContext ;
@@ -94,6 +96,13 @@ public HttpClient newHttpClient() throws NoSuchAlgorithmException, KeyManagement
9496 HttpClientBuilder httpClientBuilder = HttpClientBuilder .create ();
9597 httpClientBuilder .setUserAgent (userAgent );
9698
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+
97106 /*
98107 * Create a SSLContext that uses our Trust Strategy to trust all self-signed certificates.
99108 */
@@ -124,12 +133,22 @@ public HttpClient newHttpClient() throws NoSuchAlgorithmException, KeyManagement
124133 return (httpClientBuilder .build ());
125134 }
126135
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 {
128147 Date start = new Date ();
129148 String oldCookieStoreSerialized = BasicCookieStoreSerializerUtils .serializableToBase64 (httpCookieStore );
130149
131150 HttpGet httpGet = new HttpGet (url );
132- httpGet .setConfig (getRequestConfig (). build () );
151+ httpGet .setConfig (requestConfig );
133152 httpGet .addHeader (HttpHeaders .ACCEPT , "application/json, text/plain, */*" );
134153 HttpResponse httpResponse = getHttpClient ().execute (httpGet );
135154
@@ -138,6 +157,10 @@ public RequestResponse sendGet(String url) throws IOException, HttpClientExcepti
138157 return (new RequestResponse (httpResponse , start ));
139158 }
140159
160+ /*
161+ $ POST
162+ */
163+
141164 public RequestResponse sendPost (String url , String content , ContentType contentType ) throws IOException , HttpClientException , HttpServerException {
142165 Date start = new Date ();
143166 String oldCookieStoreSerialized = BasicCookieStoreSerializerUtils .serializableToBase64 (httpCookieStore );
@@ -154,12 +177,27 @@ public RequestResponse sendPost(String url, String content, ContentType contentT
154177 return (new RequestResponse (httpResponse , start ));
155178 }
156179
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 {
158196 Date start = new Date ();
159197 String oldCookieStoreSerialized = BasicCookieStoreSerializerUtils .serializableToBase64 (httpCookieStore );
160198
161199 HttpPost httpPost = new HttpPost (url );
162- httpPost .setConfig (getRequestConfig (). build () );
200+ httpPost .setConfig (requestConfig );
163201 httpPost .addHeader (HttpHeaders .ACCEPT , "application/json, text/plain, */*" );
164202 UrlEncodedFormEntity entity = new UrlEncodedFormEntity (form , Consts .UTF_8 );
165203 httpPost .setEntity (entity );
0 commit comments