此服务器的证书无效

pa1*_*a12 10 iphone nsurlconnection ios nsurlconnectiondelegate

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

- 连接:willSendRequestForAuthenticationChallenge: - 连接:canAuthenticateAgainstProtectionSpace

但我正在尝试使用

sendAsynchronousRequest:队列:completionHandler:

所以你没有得到回调.我调查了它所说的苹果文档

如果需要进行身份验证才能下载请求,则必须将所需凭据指定为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 =

Moh*_*shi 9

您正在使用的Web服务器要求服务器信任身份验证,您需要使用适当的操作正确响应.您需要实现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];
    }
}
Run Code Online (Sandbox Code Playgroud)


Bha*_*ani 7

尝试这个。

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

let session = URLSession(configuration: URLSessionConfiguration.ephemeral,
                                 delegate: self,
                                 delegateQueue: nil)
Run Code Online (Sandbox Code Playgroud)

实现以下委托回调方法:

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))
}
Run Code Online (Sandbox Code Playgroud)

  • 将此与将“允许任意负载”设置为“是”的组合对我有用 (2认同)
  • 是的,我以为人们已经同意了。但是,它确实需要启用任意负载。 (2认同)

Dai*_*jan 5

你无法用你正在尝试的方式解决它

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

一切都不好。CFNetwork 目前对于苹果来说应该没问题,但其他 2 种方法甚至不是应用商店安全的

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


sky*_*der 5

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

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

AFHTTPSessionManager * apiManager = [AFHTTPSessionManager initWithBaseURL:[NSURL URLWithString:baseURL];
AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
policy.allowInvalidCertificates = YES;
apiManager.securityPolicy = policy;
Run Code Online (Sandbox Code Playgroud)