NSURLConnection/CFURLConnection HTTP 加载失败 (kCFStreamErrorDomainSSL, -9813) iOS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23241872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813) iOS
提问by Sumit Patel
Currently I am consuming a soap web service using block in ios my source code is as follows
目前我正在使用ios中的块使用soap web服务我的源代码如下
NSString *xml = requestXMLToSent;
NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[xml length]];
NSURL *serviceURL = [NSURL URLWithString: url];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:serviceURL];
[urlRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[urlRequest addValue: serviceURL forHTTPHeaderField:@"SOAPAction"];
[urlRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[urlRequest setHTTPBody: [xml dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setHTTPMethod:@"POST"];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError == NULL) {
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
NSInteger statuscode = httpResponse.statusCode;
if (statuscode == 200) {
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"response String : %@",responseString);
}else{
NSLog(@"%@",response);
}
}else{
NSLog(@"There is an error in URL connection and the Error is : %@",connectionError);
}
I am getting the following error @ console
我收到以下错误@控制台
NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
There is an error in URL connection and the Error is : Error Domain=NSURLErrorDomain Code=-1202 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “www.xxxxxxxx.net” which could put your confidential information at risk." UserInfo=0x10948bbb0 {NSUnderlyingError=0x109470d10 "The certificate for this server is invalid. You might be connecting to a server that is pretending to be “www.xxxxxx.net” which could put your confidential information at risk.", NSErrorFailingURLStringKey=https: // www.----------------------------------, NSErrorFailingURLKey=https: // ------------------------- NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, NSURLErrorFailingURLPeerTrustErrorKey=, NSLocalizedDescription=The certificate for this server is invalid. You might be connecting to a server that is pretending to be “www.xxxxxx.net” which could put your confidential information at risk.}
URL 连接中存在错误,错误为:Error Domain=NSURLErrorDomain Code=-1202“此服务器的证书无效。您可能正在连接到一个伪装成“www.xxxxxxxx.net”的服务器,这可能将您的机密信息置于危险之中。” UserInfo=0x10948bbb0 {NSUnderlyingError=0x109470d10 “此服务器的证书无效。您可能正在连接到一个伪装成“www.xxxxxx.net”的服务器,这可能会使您的机密信息处于危险之中。”,NSErrorFailingURLStringKey=https: // www.---------------------------------, NSErrorFailingURLKey=https: // ----- -------------------- NSLocalizedRecoverySuggestion=您仍要连接到服务器吗?, NSURLErrorFailingURLPeerTrustErrorKey=, NSLocalizedDescription=此服务器的证书无效。
回答by Shanu ji
Server is throwing SSLcertificate error.
For the sake of testing, you can add the following code to the appDelegate:
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host {
return YES;
}
服务器抛出SSL证书错误。为了测试,你可以在 appDelegate 中添加以下代码:
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host {
return YES;
}
This will bypass the SSL error
这将绕过 SSL 错误
Note: works for NSURLConnection & UIWebView, but not for WKWebView
注意:适用于 NSURLConnection 和 UIWebView,但不适用于 WKWebView
Edited:
编辑:
For iOS 9, above procedure don't work. Add the following snippet in info.plist:
对于 iOS 9,上述过程不起作用。在 info.plist 中添加以下代码段:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
回答by user2260054
I assume you use https scheme in your serviceURL and your test server has problems with SSL certificate. If so and you trust it, implement next methods in your NSURLConnection
delegate's:
我假设您在 serviceURL 中使用 https 方案并且您的测试服务器有 SSL 证书问题。如果是这样并且您信任它,请在您的NSURLConnection
委托中实现下一个方法:
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if([challenge.protectionSpace.host isEqualToString:@"127.0.0.1"] /*check if this is host you trust: */ )
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}
To have a delegate initialize your NSURLConnection
for example with initWithRequest:delegate:startImmediately:
method.
让委托NSURLConnection
用initWithRequest:delegate:startImmediately:
方法初始化您的示例。
回答by amir
In iOS 9.0 add the following to info.plist
在 iOS 9.0 中将以下内容添加到 info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key><true/>
</dict>
回答by amir
Check your delegates are actually being called.
检查您的代表实际上是否被调用。
This documentation explains that your delegates may not be getting called in certain circumstances.
本文档解释了在某些情况下可能不会调用您的代表。
Important: The URL loading system classes do not call their delegates to handle request challenges unless the server response contains a WWW-Authenticate header. Other authentication types, such as proxy authentication and TLS trust validation do not require this header.
重要提示:除非服务器响应包含 WWW-Authenticate 标头,否则 URL 加载系统类不会调用它们的委托来处理请求挑战。其他身份验证类型(例如代理身份验证和 TLS 信任验证)不需要此标头。