Skip to content
This repository was archived by the owner on May 3, 2023. It is now read-only.

Commit 9da5db5

Browse files
add: redirect strategy to follow redirect in POST
1 parent 95ce8c5 commit 9da5db5

File tree

5 files changed

+97
-10
lines changed

5 files changed

+97
-10
lines changed

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
</dependency>
1818
<!-- Apache Httpclient -->
1919
<dependency>
20-
<groupId>org.apache.httpcomponents.client5</groupId>
21-
<artifactId>httpclient5</artifactId>
22-
<version>5.2-beta1</version>
20+
<groupId>org.apache.httpcomponents</groupId>
21+
<artifactId>httpclient</artifactId>
22+
<version>4.5.13</version>
2323
</dependency>
2424
<!-- Json -->
2525
<dependency>

src/main/java/net/httpclient/wrapper/session/HttpClientSession.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
import org.apache.http.impl.client.BasicCookieStore;
2727
import org.apache.http.impl.client.BasicCredentialsProvider;
2828
import org.apache.http.impl.client.HttpClientBuilder;
29+
import org.apache.http.impl.client.LaxRedirectStrategy;
2930
import org.apache.http.ssl.SSLContextBuilder;
31+
import org.jetbrains.annotations.NotNull;
3032
import org.json.JSONObject;
3133

3234
import 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);

src/main/java/net/httpclient/wrapper/session/HttpClientSessionAsync.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
import org.apache.http.entity.ContentType;
2020
import org.apache.http.impl.client.BasicCredentialsProvider;
2121
import org.apache.http.impl.client.HttpClientBuilder;
22+
import org.apache.http.impl.client.LaxRedirectStrategy;
2223
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
2324
import org.apache.http.ssl.SSLContextBuilder;
25+
import org.jetbrains.annotations.NotNull;
2426

2527
import javax.net.ssl.SSLContext;
2628
import java.io.IOException;
@@ -71,6 +73,13 @@ public HttpClient newHttpClient() throws NoSuchAlgorithmException, KeyManagement
7173
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
7274
httpClientBuilder.setUserAgent(getUserAgent());
7375

76+
/*
77+
* This line below allow redirection (301, 302, 303, 307, 308) to be followed automatically
78+
* in the case of a POST request. By default, the HttpClient does not follow redirections for POST requests.
79+
* This is specified in the HTTP specification. (HTTP RFC 2616)
80+
*/
81+
httpClientBuilder.setRedirectStrategy(new LaxRedirectStrategy());
82+
7483
/*
7584
* Create a SSLContext that uses our Trust Strategy to trust all self-signed certificates.
7685
*/
@@ -120,7 +129,7 @@ public HttpClient newHttpClient() throws NoSuchAlgorithmException, KeyManagement
120129
}
121130

122131
@Override
123-
public RequestResponse sendGet(String url) throws IOException, HttpClientException, HttpServerException {
132+
public @NotNull RequestResponse sendGet(@NotNull String url) throws IOException, HttpClientException, HttpServerException {
124133
try {
125134
semaphore.acquire();
126135
RequestResponse result = super.sendGet(url);
@@ -146,7 +155,7 @@ public RequestResponse sendPost(String url, String content, ContentType contentT
146155
}
147156

148157
@Override
149-
public RequestResponse sendForm(String url, List<NameValuePair> form) throws IOException, HttpClientException, HttpServerException {
158+
public @NotNull RequestResponse sendForm(@NotNull String url, @NotNull List<NameValuePair> form) throws IOException, HttpClientException, HttpServerException {
150159
try {
151160
semaphore.acquire();
152161
RequestResponse response = super.sendForm(url, form);

src/main/java/net/httpclient/wrapper/session/HttpClientSessionRateLimited.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public RequestResponse sendPost(String url, String content, ContentType contentT
3232
}
3333

3434
@Override
35-
public RequestResponse sendGet(String url) throws IOException, HttpClientException, HttpServerException {
35+
public @NotNull RequestResponse sendGet(@NotNull String url) throws IOException, HttpClientException, HttpServerException {
3636
rateLimiter.acquire();
3737
return (super.sendGet(url));
3838
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package net.httpclient.wrapper.ratelimiter;
2+
3+
import net.httpclient.wrapper.exception.HttpClientException;
4+
import net.httpclient.wrapper.exception.HttpException;
5+
import net.httpclient.wrapper.exception.HttpServerException;
6+
import net.httpclient.wrapper.response.RequestResponse;
7+
import net.httpclient.wrapper.session.HttpClientSession;
8+
import org.apache.http.NameValuePair;
9+
import org.apache.http.client.config.RequestConfig;
10+
import org.apache.http.message.BasicNameValuePair;
11+
import org.junit.jupiter.api.Assertions;
12+
import org.junit.jupiter.api.Test;
13+
14+
import java.io.IOException;
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
20+
public class HttpClientSessionTest {
21+
22+
@Test
23+
public void testRedirectN() throws IOException, HttpServerException {
24+
try {
25+
HttpClientSession httpClientSession = new HttpClientSession();
26+
String url = "http://t.co/I5YYd9tddw";
27+
RequestConfig requestConfig = RequestConfig.custom()
28+
.setRedirectsEnabled(true)
29+
.build();
30+
List<NameValuePair> params = new ArrayList<>();
31+
params.add(new BasicNameValuePair("url", "https://www.wikipedia.net/"));
32+
RequestResponse requestResponse = httpClientSession.sendForm(url, params, requestConfig);
33+
assertEquals(200, requestResponse.getStatusCode());
34+
} catch (HttpException e) {
35+
System.out.println(e.getRequestResponse().getRawResponse());
36+
Assertions.fail();
37+
}
38+
}
39+
40+
}

0 commit comments

Comments
 (0)