2016-09-26 1 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로드가 사람이 issue.Please 가이드를 직면

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

NSURLSession

실패 얻을.

+0

위의 메시지 수신하지 않았다, 그것은 OS의 같은 버전에있는 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];   
    } 
}