iOS 網絡請求信任全部證書

網絡請求信任全部證書網絡

一、AFNetWorking 設置allowInvalidCertificates 便可session

    

AFHTTPSessionManager  *afnManager = [AFHTTPSessionManager manager];
 // 客戶端是否信任非法證書
 afnManager.securityPolicy.allowInvalidCertificates = YES;
 // 是否在證書域字段中驗證域名
 [afnManager.securityPolicy setValidatesDomainName:NO];

二、ASIHTTPRequest 設置setValidatesSecureCertificate爲NOapp

 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
       
 [request setValidatesSecureCertificate:NO];
 [request startSynchronous];

三、使用原生NSURLSession,url

 NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]delegate:self delegateQueue:[NSOperationQueue mainQueue]];spa

在改類中設置代理,遵循代理協議,實現代理方法.net

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler代理

//請求方法
NSString * urlstr = [NSString stringWithFormat:@"%@x x x x x x",HttpServerStr]; NSDictionary* parameters = @{@"loginname":_userNameTextField.text,@"password":_passWordTextField.text}; NSData *paramData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:nil]; //訪問請求 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlstr]]; request.timeoutInterval = TimeoutInterval; request.HTTPMethod = @"POST"; request.HTTPBody = paramData; [request setValue:[NSString stringWithFormat:@"application/x-www-form-urlencoded;charset=UTF-8"] forHTTPHeaderField:@"Content-Type"]; // NSURLSession *session = [NSURLSession sharedSession]; NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]]; NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSLog(@"請求結束"); if (error == nil) { NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; _userManager.h5_user_token = [dic objectForKey:@"user_token"]; NSLog(@"h5-userData--:%@",dic); NSLog(@"error:%@",error); } //get dashboardData [self makeDashBoardData]; }]; [dataTask resume];

//代理方法code

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler{
    
    /*方法一 信任全部證書*/
    if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
        NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
        if(completionHandler)
            completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
    }
}

 參考:orm

iOS HTTPS證書不受信任解決辦法server

相關文章
相關標籤/搜索