iOS小技能: HTTPS问题分析汇总

195 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第10天,点击查看活动详情

引言

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

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

解决方法,修改超时时间

-    request.timeoutInterval = 5.0;
+    request.timeoutInterval = 30.0;

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

针对 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;

在这里插入图片描述

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

                disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge;

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

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

  • 设置securityPolicy

AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
securityPolicy.validatesDomainName = NO;
securityPolicy.allowInvalidCertificates = YES;
manager.securityPolicy = securityPolicy;

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

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

see also

更多内容请关注:【gzh:iOS逆向】