HTTPS URLConnection and redirect to 302 using HTTP

I use a secure synchronous URLConnection to request a webpage from a server (e.g. URLConnection.sendsync(“GET”, “https://…, 20)

The response is an expected Redirect (httpstatus 302) with an unexpected http location (e.g. http://…). I would have thought the server would send an HTTPS redirect. Anyways, https is the location of the redirect, however, URLConnection automatically connects to http.

The same request in any browser works, meaning, it automatically redirects to https, not http.

Am I missing something with URLConnection? Should it not work the same was as the browser? (Meaning automatically redirect to https, or fallback to https)


URLConnection is not a browser, so you need to action the redirect yourself.

Yeah, interesting. In that case, I would have thought it would not have redirected automatically to the non-secure location, so I thought maybe I missed something that would step-up to https.

I guess we can always subclass URLConnection and add our own automatic redirect logic. I also work in the .NET space and they have a property in their base class to follow a redirect. You just set it toTrue and call it a day.