此服务器的证书无效



我知道如果我使用下面的nsurconnectiondelegate,它将被修复

–连接:willSendRequestForAuthenticationChallenge:–连接:canAuthenticateAgainstProtectionSpace

但我正在尝试使用

sendAsynchronousRequest:队列:完成处理程序:

所以你不会得到回调。我查看了之后的苹果文档

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

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

+(void)setAllowsAnyHTTPSCertificate:(BOOL)inAllowforHost:(NSString*)inHost;

知道怎么做吗?

以下是我得到的错误

此服务器的证书无效。您可能正在连接到假装为"example.com=0x8b34da0"的服务器{NSErrorFailingURLStringKey=https://example.com/test/,NSLocalizedRecovery Suggestion=是否要连接到服务器无论如何NSErrorFailingURLKey=https://example.com/test/,NSLocalizedDescription=此服务器的证书无效。你可能正在连接到一个伪装成"example.com"的服务器这可能会使您的机密信息处于危险之中。,NSUnderlyingError=0x26c1c0"此服务器的证书为无效的你可能正在连接一个假装是"example.com",这可能会使您的机密信息面临风险。",NSURLErrorFailingURLPerTrustErrorKey=

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

更多信息可在此处找到:-https://developer.apple.com/library/ios/technoltes/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];
}
}

试试这个。

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

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))
}

您无法用尝试的方式修复它

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

都不好CFNetwork目前对苹果来说应该还可以,但其他两种方法甚至都不是应用商店安全的

更好修复服务器。这是最简单、最干净的

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

(只是一个临时客户端解决方案!)

AFHTTPSessionManager * apiManager = [AFHTTPSessionManager initWithBaseURL:[NSURL URLWithString:baseURL];
AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
policy.allowInvalidCertificates = YES;
apiManager.securityPolicy = policy;

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

在我的情况下,发生此错误是因为我的防火墙阻止了所需的url。在删除防火墙限制后,它运行良好

这是一个证书错误。您需要更改设置,以便您的程序/os忽略证书,或者将url/certificate添加到受信任列表中。

抱歉,这是身份验证,证书是身份验证。我看了一眼,发现了这篇文章。

不确定它是否能解决您的问题,但它基本上说明,它们不包括如何在文档中使用证书连接到网站的情况。

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

在我的案例中,此错误是由于我的系统日期造成的。它被设置为旧日期,证书从该旧日期起无效。更正日期后,它就起作用了。

最新更新