这个问题看起来重复,我搜索了问题的解决方案。 其中很少有人建议,在应用以下建议后仍然无效
<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&password=pqr@2&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];
有人遇到过问题。请指导。