Reputation: 1413
I have a URL with format schema://hostname (e.g. https://some.server.com).
I'm trying to get the redirect page (in my case to a login form) (e.g. httpx://some.server.com/this/is/you/loginpage)
Browsers will automatically redirect to this page, which is received from the server by a 'Location' header in the response. When analysing this request in Google Chrome, I see a '302' status with a location header. This location header has the correct URL, but for some reason I'm not able to retrieve this header (or http status!) using the NSURLConnectionDelegate methods. I've also tried AFNetworking, but with same results.
In this case the connection:willSendRequest:redirectResponse: method is only called for canonical changes, but not for location changes.
I also found out the UIWebView DOES automatically redirect to this 'Location' URL. I'm also able to catch this redirect using the UIWebView delegate method: webView:shouldStartLoadWithRequest:navigationType: I don't want to use UIWebView since this is a UI component which is only accessible in the main thread, while I'm doing some background operations. (However I'm using it now as a workaround).
The desired behaviour is also described here: http://en.wikipedia.org/wiki/HTTP_location Any ideas how to detect/perform this redirect using NSURLConnection (or any AFNetworking class)?
Relevant code using NSURLConnection (does not redirect in this situation):
- (void)resolveUsingNSURLConnection:(NSURL *)URL {
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
self.isRequestBusy = YES;
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (urlConnection) {
[self waitUntilRequestFinished];
}
NSLog(@"resolveUsingNSURLConnection ready (status: %i, URL: %@)", self.response.statusCode, self.response.URL);
}
- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse {
if (redirectResponse) {
// Just log the redirect request
NSLog(@"[%@] server redirect allowed:\n\t%@ %@\n\t%@ %@", NSStringFromClass([self class]), self.request.HTTPMethod, self.request.URL.absoluteString, request.HTTPMethod, request.URL.absoluteString);
return request;
} else {
// Just log the canonical change
NSLog(@"[%@] canonical change:\n\t%@ %@\n\t%@ %@", NSStringFromClass([self class]), self.request.HTTPMethod, self.request.URL.absoluteString, request.HTTPMethod, request.URL.absoluteString);
return request;
}
}
Relevant code using UIWebView (has desired redirect behaviour, not the desired component):
- (void)resolveUsingUIWebView:(NSURL *)URL {
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:@selector(resolveUsingUIWebView:) withObject:URL waitUntilDone:YES];
return;
}
NSURLRequest *hostnameURLRequest = [NSURLRequest requestWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f];
self.isRequestBusy = YES;
UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectZero];
webview.delegate = self;
[webview loadRequest:hostnameURLRequest];
[self waitUntilRequestFinished];
NSLog(@"resolveUsingUIWebView ready (status: UNKNOWN, URL: %@)", webview.request.URL);
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(@"webView:shouldStartLoadWithRequest: %@ (%i)", request.URL, navigationType);
return YES;
}
Upvotes: 1
Views: 1927
Reputation: 112857
There are several ways UIWebView handles redirects differently that I have seen, a couple are:
The web service may examine the User-Agent string and act differently.
The redirect may be JaveScript.
Upvotes: 2