2016-11-22 41 views
0

我已经使用NSMutableURLRequest很长时间来连接到我的服务器。为什么切换到HTTPS时,我通过NSURLRequest获得了authenticationchallenge?

为了避免双重roadtrips,我设置的USR /密码马上在头,像这样:

NSMutableURLRequest *request = [NSMutableURLRequest 
      requestWithURL:[NSURL URLWithString:url] 
       cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:HTTP_REQUEST_TIMEOUT]; 
NSString *authStr = [NSString stringWithFormat:@"%@:%@", inUsr, inPwd]; 
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [[authStr dataUsingEncoding:NSISOLatin1StringEncoding] base64EncodedStringWithOptions:0]]; 

[request setValue:authValue forHTTPHeaderField:@"Authorization"]; 

这一直很好,在“willSendRequestForAuthenticationChallenge”是从来没有所谓的,除非有一些错误,这样的方法一直看起来像:

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {   
    NSDictionary *errorInfo = ((NSHTTPURLResponse *) challenge.failureResponse).allHeaderFields; 

    NSError *error; = [NSError errorWithDomain:@"httprequesthandler" code:WRONG_CREDENTIALS userInfo:errorInfo]; 
    [delegate finishedWithErrors:error]; 

但是现在,我使用的是相同的URL的一如既往,只有“https”开头的不是“http”,突然这个方法被调用每次。

我希望我的请求按照常规工作,即填充基本头文件和只有一个请求到服务器。

我不确定我错过了什么,所以指针将非常感谢!

回答

0

使用https作为您的方案(或协议)要求安全地进行连接,既要对传输的数据进行加密,也要向您提供有关您连接的服务器的真实性的信息。

在此处调用的委托方法(connection:willSendRequestForAuthenticationChallenge:)与您在服务器上进行身份验证无关,但服务器会与您进行身份验证。如果您深入了解挑战对象(NSURLAuthenticationChallenge),您可以找到服务器提供的凭据,让您知道它是您实际尝试连接的服务器,而不是冒名顶替者。

通常情况下,你不需要使用这种方法,除非你想以超越操作系统已经做的事情来验证服务器。

+0

好的。谢谢! – Mathias

相关问题