Resolve a url after a #302 redirect #Xamarin.MAC
Say you have a url, like bit.ly/whatever – then you might want to know what the final url of that url leads to – I wanted to do that using Xamarin.MAC -although I assume that Xamarin.IOS would be the same.
NSUrl url = new NSUrl (strUrl);
NSUrlRequest request = new NSUrlRequest(url);
NSUrlResponse response = null;
NSError error = null;
NSUrlConnection.SendSynchronousRequest(request,out response,out error);
if (error != null)
{
UITools.MessageBox(error.ToString());
}
url = response.Url;
UITools.MessageBox(url.ToString());
The Objective C code that I copied this from was as follows:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:orinigalURL
* cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:15.0];
[request setHTTPMethod:@“HEAD“];
NSURLResponse *response = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSURL *finalURL = response.URL;
As it turned out, the error was actually that I had to add this to my info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>