6

When using a URLConnection, the 301 redirect doesn't work, doesn't even show a Location header, using getHeaderFields(). It is a blank list, except in newer Android (I tested 4.1 and it worked). It looks like something this has been reported in the default browser here as well, though in my test it worked in the Android browser. Is there some workaround for this bug in older Android?

I tried:

URLConnection conn = u.openConnection();
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
(conn).setInstanceFollowRedirects(true);

but it still returns an empty list, except in newer Android.

Update: It may be a related issue, it seems sometimes the URLConnection isn't even sending a request in some cases. (I checked with Wireshark on a pc with emulator). Is there a way to work-around this bug?

Update: I tried testing for 3xx redirect, redirects worked fine, but normal links didn't work with Ian's Cookie Manager. After making sure the setCookies was called directly after openConnection, it works great:

        URL u = new URL(_url);
        ...
        int tries = 4;
        int code = 301;
        URLConnection conn = null;
        while (tries > 0 && code/100 == 3) {
            conn = null;
            conn = u.openConnection();
            _CM.setCookies(conn);
            ((HttpURLConnection)conn).setInstanceFollowRedirects(false);//Required
            code =((HttpURLConnection)conn).getResponseCode();
            if (code/100 == 3) {
                String loc = conn.getHeaderField("Location");
                u = new URL(loc);
            }
        }

        //conn.addRequestProperty("Accept-Encoding", "gzip");

        conn.connect();
        _CM.storeCookies(conn);

The really strange thing is, for newer Android (4.1 emulator) the FollowRedirect line (commented "Required") is not necessary. On older Android (2.2), it gives Connection Reset by Peer error. This was probably the reason my redirect experimental code was failing on 2.2, not 4.1. Any reason for the differences in functionality? According to comments here, redirection https apparently has different behavior depending on the JVM version, could it be that Android's URLConnection/HTTPUrlConnection has changed in different versions as well?

Community
  • 1
  • 1
NoBugs
  • 9,310
  • 13
  • 80
  • 146
  • You said HTTP request sometimes doesn't reach the server, what is the HTTP response code `conn.getResponseCode();` when redirect does't happen? – yorkw Nov 06 '12 at 03:25
  • The method getResponseCode() is undefined for the type URLConnection – NoBugs Nov 06 '12 at 03:32
  • 1
    Using `HttpURLConnection.getResponseCode();` when dealing with http related problem, I would always get and check the actual response code before doing anything further. – yorkw Nov 06 '12 at 20:09
  • Apparently you have to re-set the `conn` after each redirect, was why double redirect wasn't working. Good workaround. Why does the default behavior not include this though? – NoBugs Nov 07 '12 at 02:30
  • Try using `HttpURLConnection.setFollowRedirects();`, [Android API Doc](http://developer.android.com/reference/java/net/HttpURLConnection.html) is not accurate, check out [Java API Doc](http://docs.oracle.com/javase/1.5.0/docs/api/java/net/HttpURLConnection.html) too see the difference between these two methods. – yorkw Nov 07 '12 at 03:13
  • Good idea, it seems to work for all redirects now but not normally, unfortunately. (See code sample) – NoBugs Nov 07 '12 at 06:50
  • What method you are using `HttpURLConnection.getRequestMethod();` GET or POST? – yorkw Nov 07 '12 at 20:27
  • A web agent regardless of HttpUrlConnection or HttpClient should by default automatically handle the redirect response if the original request use method GET or HEAD, if it doesn't, I would consider it as a defect or bug. check out [here](http://stackoverflow.com/questions/10341475/getting-url-after-a-redirect-using-httpclient-executehttpget/10342229#10342229) for more details. – yorkw Nov 08 '12 at 22:02
  • Looks like user is supposed to ok a 302 redirect (not automatic)? Thanks for the link. – NoBugs Nov 09 '12 at 03:47
  • You've provided some helpful info, @yorkw could you expand your answers to an answer below? – NoBugs Nov 13 '12 at 01:32
  • I write comments only simply because I don't have a fully overview and understanding on your question. Feel free to use any helpful pieces and answer it yourself, I would like to vote your answer if you can provide one. – yorkw Nov 13 '12 at 01:53
  • The code above is the "answer", the question now is why does Android do this? Anyway I can't bounty my own answer to the question – NoBugs Nov 13 '12 at 01:58

1 Answers1

2

Not sure about URLConnection, but I know that HttpClient honors redirects and we use it all the way back to Android 2.1

http://developer.android.com/reference/org/apache/http/client/HttpClient.html

(Based on apache commons HttpClient)

Jason Polites
  • 5,571
  • 3
  • 25
  • 24