【发布时间】:2013-08-04 16:56:25
【问题描述】:
NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
非常非常令人沮丧!我已经为此拉了几个小时的头发。我在我的 Linode 服务器上使用自签名证书。端口是 8000,无法让它在 443 上工作。我不相信这是原因。这是我的代码,它是 99% 的样板:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.myserver.com:8000/test.json"]];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
在底部:
#pragma mark NSURLConnectionDelegate
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
NSLog(@"protectionSpace: %@", [protectionSpace authenticationMethod]);
// We only know how to handle NTLM authentication.
if([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodNTLM])
return YES;
// Explicitly reject ServerTrust. This is occasionally sent by IIS.
if([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust])
return NO;
return NO;
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
[[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"%@", response);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
NSLog(@"%@", data);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"didFailWithError");
NSLog([NSString stringWithFormat:@"Connection failed: %@", [error description]]);
}
天哪,救命!
更新
它与这个委托方法一起工作。我正在收到回复,但有问题。
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
[[challenge sender] useCredential:[NSURLCredential
credentialWithUser:@"user"
password:@"password"
persistence:NSURLCredentialPersistencePermanent] forAuthenticationChallenge:challenge];
}
我提供的“用户”和“密码”是完全随机的,服务器不会检查。 如何在我的服务器上接受连接之前验证凭据?
编辑:我正在运行 Node.js 服务器
【问题讨论】:
-
“我提供的“用户”和“密码”是完全随机的,服务器不会检查”——这是一个不同的问题。您应该将其作为一个单独的问题提出,并可能链接回该问题。 Linode 配置可能是题外话,所以你可能不得不徘徊到超级用户或服务器故障。 (您可能应该接受 Fonix 的回答,因为它似乎帮助您解决了问题)。
标签: ios objective-c node.js ssl https