2016-09-26 6 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];   
    } 
} 
関連する問題