前些天集成了阿里的httpdns服務(wù),
sdk集成完成了,但是在請求的的時候,出現(xiàn)-1002,-999,502等錯誤,我用的網(wǎng)絡(luò)庫是afn;
問了阿里的技術(shù)支持,給了幾個鏈接,然并卵…………此處一萬只曹尼瑪飄過。
為什么需要httpdns呢,防止DNS劫持。DNS劫持請自行百度。
其實這里的基本邏輯并不復(fù)雜,就是通過域名獲取ip,真正發(fā)起請求的時候把之前使用域名的地方替換為ip。在請求頭里面添加host字段就可以了。
坑一:替換以后無法通過https證書驗證
解決辦法:?跳過證書校驗過程 創(chuàng)建AFHTTPSessionManager對象時候 設(shè)置一下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;
? ? }];