Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get the redirect URL using NSURLConnection, when server returns 'Moved temporarily' or 'Moved permanently'?
    primarykey
    data
    text
    <p>I have a URL with format schema://hostname (e.g. <a href="https://some.server.com" rel="nofollow">https://some.server.com</a>).<br> 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)</p> <p>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.</p> <p>In this case the connection:willSendRequest:redirectResponse: method is only called for canonical changes, but not for location changes.</p> <p>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).</p> <p>The desired behaviour is also described here: <a href="http://en.wikipedia.org/wiki/HTTP_location" rel="nofollow">http://en.wikipedia.org/wiki/HTTP_location</a> Any ideas how to detect/perform this redirect using NSURLConnection (or any AFNetworking class)?</p> <p>Relevant code using NSURLConnection (does not redirect in this situation):</p> <pre><code>- (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; } } </code></pre> <p>Relevant code using UIWebView (has desired redirect behaviour, not the desired component):</p> <pre><code>- (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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload