前些天集成了阿里的httpdns服务,
sdk集成完成了,但是在请求的的时候,出现-1002,-999,502等错误,我用的网络库是afn;
问了阿里的技术支持,给了几个链接,然并卵…………此处一万只曹尼玛飘过。
为什么需要httpdns呢,防止DNS劫持。DNS劫持请自行百度。
其实这里的基本逻辑并不复杂,就是通过域名获取ip,真正发起请求的时候把之前使用域名的地方替换为ip。在请求头里面添加host字段就可以了。
坑一:替换以后无法通过https证书验证
解决办法: 跳过证书校验过程 创建AFHTTPSessionManager对象时候 设置一下2个block
[manager setSessionDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition(NSURLSession* _Nonnull session,NSURLAuthenticationChallenge* _Nonnull challenge,NSURLCredential*__autoreleasing _Nullable * _Nullable credential) {
NSURLSessionAuthChallengeDisposition disposition =NSURLSessionAuthChallengePerformDefaultHandling;
if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
*credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
disposition =NSURLSessionAuthChallengeUseCredential;
}else{
disposition =NSURLSessionAuthChallengePerformDefaultHandling;
}
returndisposition;
}];
[manager setTaskDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition(NSURLSession* _Nonnull session,NSURLSessionTask* _Nonnull task,NSURLAuthenticationChallenge* _Nonnull challenge,NSURLCredential*__autoreleasing _Nullable * _Nullable credential) {
NSURLSessionAuthChallengeDisposition disposition =NSURLSessionAuthChallengePerformDefaultHandling;
if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
*credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
disposition =NSURLSessionAuthChallengeUseCredential;
}else{
disposition =NSURLSessionAuthChallengePerformDefaultHandling;
}
returndisposition;
}];