Well I'm trying to find a way to get http components to follow an redirect but haven't found any on google so I've came here to ask for help The func:
public String GetSite(String site, String path) throws Exception {
HttpParams params = new SyncBasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
HttpProtocolParams.setUseExpectContinue(params, true);
HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[]{
// Required protocol interceptors
new RequestContent(),
new RequestTargetHost(),
// Recommended protocol interceptors
new RequestConnControl(),
new RequestUserAgent(),
new RequestExpectContinue()});
HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
HttpContext context = new BasicHttpContext(null);
HttpHost host = new HttpHost(site, 80);
DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
try {
String[] targets = {
path};
for (int i = 0; i < targets.length; i++) {
if (!conn.isOpen()) {
Socket socket = new Socket(host.getHostName(), host.getPort());
conn.bind(socket, params);
}
BasicHttpRequest request = new BasicHttpRequest("GET", targets[i]);
request.setParams(params);
httpexecutor.preProcess(request, httpproc, context);
HttpResponse response = httpexecutor.execute(request, conn, context);
response.setParams(params);
httpexecutor.postProcess(response, httpproc, context);
if (!connStrategy.keepAlive(response, context)) {
conn.close();
} else {
}
return (EntityUtils.toString(response.getEntity()));
}
} finally {
conn.close();
}
return null;
}
Any help with this too? because I can't find anything...
By default, following the HTTP Spec, the HttpClient will automatically follow redirects.
Java Http Redirect Example If a server is redirected from the original URL to another URL, the response code should be 301: Moved Permanently or 302: Temporary Redirect. And you can get the new redirected url by reading the “Location” header of the HTTP response header.
To follow redirect with Curl, use the -L or --location command-line option. This flag tells Curl to resend the request to the new address. When you send a POST request, and the server responds with one of the codes 301, 302, or 303, Curl will make the subsequent request using the GET method.
Class LaxRedirectStrategy Lax RedirectStrategy implementation that automatically redirects all HEAD, GET, POST, and DELETE requests. This strategy relaxes restrictions on automatic redirection of POST methods imposed by the HTTP specification.
I would recommend you to use the basic DefaultHttpClient
which supports redirection without any tuning or additional code. Its behavior can be controlled by 4 parameters described in section 5.2 HttpClient parameters.
If you really do not want to depend on httpcomponents-client
but only on httpcomponents-core
, you will have to pick up implementation from org.apache.http.impl.client.RedirectStrategy
and create your own code to parse response, determine if it is a valid redirection (read HTTP spec carefully, that is not so simple) and triggers a new request in loop if needed.
My opinion: no reason to write such a complex code again, and one day authentication will be required and you will have to add that support too. So, just rely on HttpClient.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With