【发布时间】:2017-04-06 00:37:20
【问题描述】:
我已经使用 NSMutableURLRequest 连接服务器很长时间了。
为了避免双路旅行,我立即在标题中设置了 usr/pwd,如下所示:
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”,而且突然每次都会调用这个方法。
我希望我的请求能够正常工作,即填充基本标头并且只向服务器发出一个请求。
我不确定我缺少什么,因此非常感谢您的指点!
【问题讨论】:
标签: ios objective-c https nsurlconnection