Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa touch 使用NSURLConnection忽略证书错误_Cocoa Touch_Cocoa_Nsurlconnection - Fatal编程技术网

Cocoa touch 使用NSURLConnection忽略证书错误

Cocoa touch 使用NSURLConnection忽略证书错误,cocoa-touch,cocoa,nsurlconnection,Cocoa Touch,Cocoa,Nsurlconnection,我得到了这个错误 The certificate for this server is invalid. You might be connecting to a server that is pretending to be "server addres goes here" which could put your confidential information at risk." 我使用的方法是: [NSURLConnection sendSynchronousRequest:requ

我得到了这个错误

The certificate for this server is invalid. You might be connecting to a server
that is pretending to be "server addres goes here" which could put your
confidential information at risk."
我使用的方法是:

[NSURLConnection sendSynchronousRequest:request
                      returningResponse:&response
                                  error:&error];
我怎样才能解决这个问题

我尝试了以下代码:

 NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request
                                                               delegate:self];

但是我在didReceiverResponse方法中获得了EXC\u BAD\u访问权限。

您使用的是HTTPS url吗。如果是,请转到浏览器中的url并检查证书。确保证书对您尝试使用的域或子域有效,并且服务器上已安装所有中间证书。我遇到了与此类似的问题,解决方案是安装中间证书。

如果不发送任何敏感信息,您可以忽略无效的证书。描述了如何做到这一点。下面是本文中描述的方法之一的示例实现

实际上,您希望在
@实现的正上方定义一个虚拟接口:

@interface NSURLRequest (DummyInterface)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host;
@end
在调用
sendSynchronousRequest
之前,调用在虚拟接口中定义的私有方法:

[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[URL host]];

您已将委托设置为self,因此可以通过在发送该请求的类中实现NSURLConnectionLegate的一部分来解决此问题

实施这一点:

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
通过这样做:

[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];

注意:这不是用于生产的解决方案。证书存在是有原因的!:)

这并不能回答您关于“如何忽略错误”的问题。那是不负责任的

相反,它向您展示了如何加载认证服务器的CA,以便连接可以按预期进行。下面,CA与应用捆绑在一起,称为
CA-cert.der
。它是一个DER(与PEM相反)编码的证书

另外,你可能想看看苹果的
ksectrustsresultunspecified
成功,请参阅

正确的(非弃用、非私有)方式使用新的:

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
apple为NSURLConnectionLegates指定的方法是使用为保护空间提供的凭据响应ServerTrust方法(这将允许您连接:

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
    {
        NSLog(@"Ignoring SSL");
        SecTrustRef trust = challenge.protectionSpace.serverTrust;
        NSURLCredential *cred;
        cred = [NSURLCredential credentialForTrust:trust];
        [challenge.sender useCredential:cred forAuthenticationChallenge:challenge];
        return;
    }

    // Provide your regular login credential if needed...
}
对于每个可用的身份验证方法,将多次调用此方法一次,如果您不想使用该方法登录,请使用:

 [challenge.sender rejectProtectionSpaceAndContinueWithChallenge:challenge];

我也有类似的问题。通过使用下面的代码片段解决了这个问题:

-(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {

    NSLog(@"This will execute successfully!");
    if ([[challenge protectionSpace] authenticationMethod] == NSURLAuthenticationMethodServerTrust) {

        [[challenge sender] useCredential:[NSURLCredential credentialForTrust:[[challenge protectionSpace] serverTrust]] forAuthenticationChallenge:challenge];
    }
}
由于不推荐使用以下方法:

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)space { ... }

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { ... }

这个链接使用私有api,这样我的应用会被苹果拒绝吗?如果是这样的话,我就不能使用它,因为我必须把我的应用放在appstore上。还有其他方法吗?我想这篇文章准确地回答了你的问题:不,这不起作用,DidReceiveResponse是在canAuthenticateAgainstProtectionSpace方法及其cras之前首先被调用的使用DidReceiveResponse方法。这已在apple文档中说明。请检查:简介部分回答正确,与
sendSynchronousRequest:
完美配合。显然,仅应在测试/调试时使用(显然,任何生产服务器都应具有适当的SSL证书),使用几个
#ifdef DEBUG
块即可轻松完成。“使用NSURLConnection忽略证书错误”-与其忽略错误,不如解决问题。如果您不打算正确使用PKI和SSL,那么为什么还要使用它呢?这很好,但它不会处理用户添加到存储中的证书。但是使用预加载的CA列表是危险的,因为CA Zoo中的任何CA都可以声明对站点进行认证(即使是错误的CA,比如那些为拦截颁发证书的CA)。我认为最好指定应该对站点进行认证的CA,这样坏的CA就不能破坏通道。很好的参考和示例。下面是介绍使用较新的URLSessionElegate方法的示例。
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)space { ... }

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { ... }