14

我知道如果我使用以下 nsurlconnectiondelegate 它将被修复

– 连接:willSendRequestForAuthenticationChallenge: – 连接:canAuthenticateAgainstProtectionSpace

但我正在尝试使用

发送异步请求:队列:完成处理程序:

所以你没有得到回调。我查看了苹果文档,它说如下

如果需要身份验证才能下载请求,则必须将所需凭据指定为 URL 的一部分。如果身份验证失败或缺少凭据,则连接将尝试在没有凭据的情况下继续。

我不知道该怎么做。当我抬头时,我得到的只是这个私人电话

+(void)setAllowsAnyHTTPSCertificate:(BOOL)inAllow forHost:(NSString *)inHost;

知道怎么做吗?

以下是我得到的错误

此服务器的证书无效。您可能正在连接到一个伪装成“example.com=0x8b34da0 {NSErrorFailingURLStringKey= https://example.com/test/ , NSLocalizedRecoverySuggestion=您想连接到服务器吗?, NSErrorFailingURLKey= https:// /example.com/test/ , NSLocalizedDescription=此服务器的证书无效。您可能正在连接到冒充“example.com”的服务器,这可能会使您的机密信息面临风险。NSUnderlyingError=0xa26c1c0 "此服务器的证书无效。您可能正在连接到冒充的服务器是“example.com”,这可能会使您的机密信息面临风险。", NSURLErrorFailingURLPeerTrustErrorKey=

4

8 回答 8

11

您正在使用的网络服务器要求进行服务器信任身份验证,您需要使用适当的操作正确响应。您需要实现connection:willSendRequestForAuthenticationChallenge:委托方法并使用 SecTrustRef 对其进行身份验证。

更多信息可以在这里找到:- https://developer.apple.com/library/ios/technotes/tn2232/_index.html

这是我修复错误的代码:

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    NSURLProtectionSpace *protectionSpace = [challenge protectionSpace];

    id<NSURLAuthenticationChallengeSender> sender = [challenge sender];

    if ([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust])
    {
        SecTrustRef trust = [[challenge protectionSpace] serverTrust];

        NSURLCredential *credential = [[NSURLCredential alloc] initWithTrust:trust];

            [sender useCredential:credential forAuthenticationChallenge:challenge];
    }
    else
    {
        [sender performDefaultHandlingForAuthenticationChallenge:challenge];
    }
}
于 2014-08-30T09:52:00.447 回答
5

如果您正在使用AFNetworking,则可以使用以下代码:

(就像一个临时的客户端解决方案!)

AFHTTPSessionManager * apiManager = [AFHTTPSessionManager initWithBaseURL:[NSURL URLWithString:baseURL];
AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
policy.allowInvalidCertificates = YES;
apiManager.securityPolicy = policy;
于 2014-04-23T15:41:23.713 回答
5

尝试这个。

使用自定义会话配置启动会话,如下所示:

let session = URLSession(configuration: URLSessionConfiguration.ephemeral,
                                 delegate: self,
                                 delegateQueue: nil)

实现以下委托回调方法:

public func urlSession(_: URLSession, task _: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
    guard let serverTrust = challenge.protectionSpace.serverTrust else {
        return completionHandler(URLSession.AuthChallengeDisposition.useCredential, nil)
    }
    return completionHandler(URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
}
于 2020-04-29T06:08:05.760 回答
4

你不能用你尝试的方式来解决它

  • 要么下降到 CFNetworking 以允许错误的证书
  • 将 NSConnection 与委托和 undoc'd 方法一起使用
  • 使用您找到的私有 API

都不好。CFNetwork 现在必须对苹果没问题,但其他两种方法甚至都不是应用商店安全的

最好把服务器修好。那是最简单和最干净的

于 2014-01-18T23:14:00.233 回答
1

就我而言,发生此错误是因为我的防火墙阻止了所需的 url。解除防火墙限制后一切正常

于 2017-08-10T04:55:22.910 回答
1

此问题无法通过您尝试使用块的方式来解决。您需要设置委托并实施身份验证质询委托以绕过证书验证。最好的解决方案是要么创建一个正确的证书(确保它不是自签名的),要么如果你对它没问题,将协议更改为 HTTP。

于 2016-01-07T09:31:48.183 回答
0

那是证书错误。您需要更改设置,以便您的程序/操作系统忽略证书,或将 url/证书添加到受信任列表中。

对不起,这是身份验证,证书就是身份验证。我看了一下,找到了这篇文章。

不确定它是否会解决您的问题,但它基本上指出,它们不包括通过文档中的证书连接到站点的情况。

http://www.cocoanetics.com/2009/11/ignoring-certificate-errors-on-nsurlrequest/

于 2014-01-17T23:27:45.823 回答
0

就我而言,由于我的系统日期而发生此错误。它被设置为旧日期,并且证书从该旧日期起无效。更正日期后,它可以工作。

于 2020-03-27T03:18:37.027 回答