2016-09-26 78 views
0

這個問題看起來是重複的,我已經搜索瞭解決方案的問題。 他們中很少有人提出,應用下面的建議後,仍然無法正常工作獲取NSURLSession的錯誤「NSURLSession/NSURLConnection HTTP加載失敗(kCFStreamErrorDomainSSL,-9813)」

<key>NSAppTransportSecurity</key> 
<dict> 
    <!--Include to allow all connections (DANGER)--> 
    <key>NSAllowsArbitraryLoads</key> 
    <true/> 
</dict> 

我的代碼以NSURLConnection的工作。服務器發送響應

-(NSMutableURLRequest *) getRequest 
{ 
    NSURL *URL = [NSURL URLWithString:@"https://URLRequest"]; 
    NSString *requestBody = @"username=abc&[email protected]&deviceId=646D4945-DA22-408B-B132-EFDED5430650&deviceTypeId=1&deviceName=iPad Simulator&versionInfo=Version 1.16.6 1.0.13"; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; 
    [request setHTTPMethod:@"POST"]; 

    //NSData *paramData = [requestBody dataUsingEncoding:NSUTF8StringEncoding]; 
    [request setHTTPBody:[requestBody dataUsingEncoding:NSUTF8StringEncoding]]; 
    [request addValue:@"1.0" forHTTPHeaderField:@"HTTP"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    return request; 
} 

-(void) sessionRequest 
{ 
    [NSURLConnection sendAsynchronousRequest:[self getRequest] 
            queue:[NSOperationQueue mainQueue] 
         completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { 

          if(data) 
          { 
           NSError *jsonError = nil; 
           id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError]; 
           if (jsonError) 
           { 
            NSLog(@"Problem for download data"); 
           } 
           else 
           { 
            NSLog(@"Response:%@",json); 
           } 
          }      
         }]; 
} 

我得到消息NSURLSession/NSURLConnection的HTTP負載失敗 NSURLSession

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration]; 
NSURLSession *session = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]]; 
//NSURLSession *session = [NSURLSession sharedSession]; 
NSURLSessionDataTask *task = [session dataTaskWithRequest:[self getRequest] 
             completionHandler: 
           ^(NSData *data, NSURLResponse *response, NSError *error) { 
            NSHTTPURLResponse *tempResponse = (NSHTTPURLResponse *)response; 
            NSLog(@"statusCode:%d %@",tempResponse.statusCode,tempResponse.allHeaderFields); 
            if(data) 
            { 
             NSError *jsonError = nil; 
             id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError]; 
             if (jsonError) 
             { 
              NSLog(@"Problem for download data"); 
             } 
             else 
             { 
              NSLog(@"Response:%@",json); 
             } 
            } 
           }]; 

[task resume]; 

有任何人面臨issue.Please指南。

+0

只是爲了澄清,其與NSURLConnection的工作在同一個版本的操作系統? – dgatwood

+0

是的,它適用於相同版本的iOS9.3模擬器和iPad設備。 –

+0

請嘗試在下面的問題中提到的答案https://stackoverflow.com/questions/39704663/getting-error-with-nsurlsession-nsurlsession-nsurlconnection-http-load-failed?noredirect=1#comment66711430_39704663 – sandy

回答

0

經過搜索後,發現論壇上發佈了大量答案。我面臨的問題NSURLSession/NSURLConnection HTTP加載失敗(kCFStreamErrorDomainSSL,-9802) 我正在使用NSURLConnection連接服務器。 隨着下面的代碼的幫助下,我沒有收到上述消息

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{ 

NSLog(@"challenge.protectionSpace.realm ::%@ \ 
     challenge.protectionSpace.receivesCredentialSecurely ::%d \ 
     challenge.protectionSpace.isProxy ::%d \ 
     challenge.protectionSpace.host ::%@ \ 
     challenge.protectionSpace.port ::%d \ 
     challenge.protectionSpace.proxyType ::%@ \ 
     challenge.protectionSpace.protocol ::%@ \ 
     challenge.protectionSpace.authenticationMethod ::%@",challenge.protectionSpace.realm,challenge.protectionSpace.receivesCredentialSecurely,challenge.protectionSpace.isProxy,challenge.protectionSpace.host,challenge.protectionSpace.port,challenge.protectionSpace.proxyType,challenge.protectionSpace.protocol,challenge.protectionSpace.authenticationMethod); 

NSLog(@"challenge.proposedCredential.user ::%@ challenge.proposedCredential.password::%@ challenge.proposedCredential.hasPassword::%d challenge.proposedCredential.certificates::%@",challenge.proposedCredential.user,challenge.proposedCredential.password,challenge.proposedCredential.hasPassword,challenge.proposedCredential.certificates); 
// first server = google.com 
// second Server = 8.8.8.8 
if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){ 
    if([challenge.protectionSpace.host isEqualToString:@"first server name"] || 
     [challenge.protectionSpace.host isEqualToString:@"second server name"]) 
    { 
     NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; 
     NSLog(@"CAME FOR AUTHENTICATION:%@",NSURLAuthenticationMethodServerTrust); 
     [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; 
    } 
} 
else if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodClientCertificate]) 
{ 
//  Case-1 
//  NSURLCredential *credential = [NSURLCredential credentialWithUser:[StaticVariables sharedSingleton].userName password:[StaticVariables sharedSingleton].password persistence:NSURLCredentialPersistenceNone]; 
//  //[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge] ; 
//  //[[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge]; 
//  [[challenge sender] performDefaultHandlingForAuthenticationChallenge:challenge]; 

//  Case-2 
     NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; 
     NSLog(@"CAME FOR AUTHENTICATION:%@",NSURLAuthenticationMethodClientCertificate); 
     [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];   
    } 
} 
相關問題