Is there a way to find out what port number a NSURLRequest
will use to satisfy a particular NSURL
?
Obviously, I could hard code port 80 for http
request and port 443 for https
requests. But surely there's a built-in way to get the port number from a URL scheme in Cocoa Touch.
The port
method on NSURL
only returns a value when the port is specified in the URL. It'll work for http://stackoverflow.com:80
, but not the more common http://stackoverflow.com
.
The context here is that I'm signing into a web service that's redirecting me(Say, from (http://webservice.invalid/path/to/api to http://webservice2.invalid:2003/path/to/api). All URLs are based on the service's base URL, which I want to update to reflect reality and avoid future redirects.
At the moment, I'm extracting the scheme and host, but hadn't considered that the port might change as well. I don't want to specify the port on future requests if I didn't get a port as part of the redirect.