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指南。
只是爲了澄清,其與NSURLConnection的工作在同一個版本的操作系統? – dgatwood
是的,它適用於相同版本的iOS9.3模擬器和iPad設備。 –
請嘗試在下面的問題中提到的答案https://stackoverflow.com/questions/39704663/getting-error-with-nsurlsession-nsurlsession-nsurlconnection-http-load-failed?noredirect=1#comment66711430_39704663 – sandy