When using URLConnection, redirect 301 does not work, it does not even show the Location header using getHeaderFields (). This is an empty list, with the exception of the new Android (I tested 4.1 and worked). It seems like this is being reported in the default browser here , although in my test it worked in the Android browser. Is there a way around this error 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 for the new Android.
Refresh . This may be a related problem, sometimes URLConnection does not even send a request in some cases. (I checked with Wireshark on a PC with an emulator). Is there any way around this error?
Update . I tried testing 3xx redirects, redirects worked fine, but normal links didn't work with Ian Cookie Manager. After making sure that setCookies is called immediately after openConnection, it works fine:
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);
Truly strange, for the new Android emulator (4.1) the FollowRedirect line (comment "Required") is not required. On older Android (2.2), it gives a Connection Reset by Peer error. This was probably the reason why my experimental redirect code did not execute on 2.2, not 4.1. Any reason for differences in functionality? According to the comments here , the https redirect seems to differ differently depending on the version of the JVM, maybe Android URLConnection / HTTPUrlConnection also changed in different versions?
source share