iOS小技能: HTTPS问题分析汇总:1、NSURLSessionTask finished with error - code: -1005(请求超时)2、Error Code=-999

时间:2024-10-14 08:06:15

文章目录

  • 引言
  • I、NSURLSessionTask finished with error - code: -1005(请求超时)
  • II、 Error Code=-999 cancelled的解决方案
    • 2.1 、原因分析
    • 2.2、解决方案: 设置`securityPolicy`允许不进行SSL证书验证
  • III、Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer corre

引言

  • -1005(请求超时)
  • -999 cancelled的解决方案:允许不进行SSL证书验证, 来规避SSL证书过期导致的请求报错
  • Dictionary换成ConCurrentDictionary,线程安全

I、NSURLSessionTask finished with error - code: -1005(请求超时)

解决方法,修改超时时间

-     = 5.0;
+     = 30.0;

  • 1
  • 2
  • 3

II、 Error Code=-999 cancelled的解决方案

  • iOS HTTPS请求Error Code=-999 cancelled的解决方案:【允许不进行SSL证书验证, 来规避SSL证书过期导致的请求报错】

针对 load failed with error Error Domain=NSURLErrorDomain Code=-999 "已取消"错误的解决方案的解决方案:

  • 允许不进行SSL证书验证, 来规避SSL证书过期导致的请求报错
  • 及时将有效的证书部署于所使用的环境中

2.1 、原因分析

SSL证书失效了, 导致此问题。

  • evaluateServerTrust:forDomain:
/**
 Whether or not the specified server trust should be accepted, based on the security policy.

 This method should be used when responding to an authentication challenge from a server.

 @param serverTrust The X.509 certificate trust of the server.
 @param domain The domain of serverTrust. If `nil`, the domain will not be validated.

 @return Whether or not to trust the server.
 */
- (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust
                  forDomain:(nullable NSString *)domain;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

在这里插入图片描述

证书无效后, 上面方法返回NO, 从而执行

                disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;

  • 1
  • 2

最终导致, 当前的请求被cancel。

2.2、解决方案: 设置securityPolicy允许不进行SSL证书验证

  • 设置securityPolicy

AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
securityPolicy.validatesDomainName = NO;
securityPolicy.allowInvalidCertificates = YES;
manager.securityPolicy = securityPolicy;
  • 1
  • 2
  • 3
  • 4
  • 5

III、Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection’s state is no longer corre

  • 错误信息:
{
	message = Operations that change non-concurrent collections must have exclusive access. A concurrent update was performed on this collection and corrupted its state. The collection's state is no longer correct.;
	data = <null>;
	code = 500;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 解决:具体的解决方案取决于代码
    例如: 接口对应服务端侧使用的是.net
Dictionary换成ConCurrentDictionary,线程安全

  • 1
  • 2