Pierre
Pierre

Reputation: 382

Enable automatic redirect on POST request with HttpClient

I have an app that should run on Android, hopefully starting from 2.3 to 4.4.2.

I am trying to make a HTTP POST request with Apache HttpClient, and I want to enable auto-redirect. I already made a code working in pure Java, with HttpClient library installed. It uses setRedirect(new LaxRedirectStrategy).

Homever, it isn't accepted in my Android project.

And none of the solutions I've encountered on the Internet is working :

httpclient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, true);

httpclient.getParams().setParameter("http.protocol.allow-circular-redirects", true);

HttpClientParams.setRedirecting(httpclient.getParams(), true);

...

Eclipse doesn't recognize the setRedirectStrategy() method, although the Android 4.4.2 library is bundled with my project.

If I had time, I would try to handle the redirection myself or, better, switch to HttpUrlConnection which seems a lot better. But this is a huge private library I made for a lot of webservices I have to access, and I don't have time to do this now. Is there a way to enable auto-redirect in a simple way ?

The entire code looks like this :

DefaultHttpClient client = new DefaultHttpClient();
client.setCookieStore(cookies);
client.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, true);
client.getParams().setParameter("http.protocol.allow-circular-redirects", true);
HttpClientParams.setRedirecting(client.getParams(), true);
client.getParams().setParameter("http.protocol.version",
            HttpVersion.HTTP_1_0) ;

Upvotes: 0

Views: 1795

Answers (1)

auracool
auracool

Reputation: 172

I ran into a similar problem ( i now use volley library for REST API) but i think adding this may help:

httpclient.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_0)

versions seemed to work differently between android and java.

Let me know if that works.God Bless.

Jonny 2 Plates

Upvotes: 1

Related Questions