Cakestep
Cakestep

Reputation: 111

HTTP components core follow redirect

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...

Upvotes: 7

Views: 1886

Answers (1)

Yves Martin
Yves Martin

Reputation: 10361

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.

Upvotes: 3

Related Questions