简体   繁体   中英

How to check if an HTTPS URL redirects to an HTTP URL?

I have a list of HTTPS URLs and I want to make sure they are accessible. I use HttpURLConnection to follow redirects by checking for any 300/301/302 responses, following the redirect, and so on until I get a 200 or anything else.

How do I check along the way that none of the redirects are to a HTTP (non-secure) endpoint?

Does it ever fail if the redirect from an HTTPS goes to HTTP, or do I have to manually check?

HttpURLConnection won't follow redirects when switching protocol (http -> https, https -> http). So you'll know when such redirects happen. If you want to be 100% safe, call

  • HttpURLConnection.setFollowRedirects(false) or
  • conn.setInstanceFollowRedirects(false) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM