问题
How do I detect whether a web page in a UIWebView needs to use HTTP or HTTPS? I have an address bar where the user can type in "www.apple.com" and then I convert this to a NSString with http:// in front of it. If Apple.com requires HTTPS though, how would this be handled? What exactly should I do? Thanks for your help!
回答1:
In most cases, a web site that requires HTTPS is set up to automatically redirect to the correct SSL port. In generally goes like this:
- Enter the URL http://www.mysite.com/ in your web browser. Because there is no port specified, it attempts to use the default port of 80 for HTTP.
- The site receives the request on port 80 and determines that you need to be using SSL, so it sends a redirect response back to the browser. That redirect response usually tells your browser to try again using HTTPS on the standard HTTPS port (443).
- Your browser re-initiates the request to the HTTPS port.
So, in the vast majority of cases, you don't need to know whether or not HTTPS is required or not. Just go to the site using http. If it requires HTTPS it will almost always redirect you there automatically.
来源:https://stackoverflow.com/questions/7211991/detect-whether-uiwebview-needs-to-use-http-or-https