【发布时间】:2016-02-10 23:53:19
【问题描述】:
我使用下面的代码通过发布请求从服务器获取数据
NSString *serverUrl=@"http://192.168.1.105:2300/api/customer";
NSString *post =[NSString stringWithFormat:@"action=%@",actionName];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:serverUrl]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error = nil; NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
但出现以下错误:
Error Domain=NSURLErrorDomain Code=-1022 "资源不能被 加载,因为应用程序传输安全策略需要使用 安全连接。” UserInfo={NSUnderlyingError=0x7f9be35f19a0
我发现几篇帖子说在 info.plist 中添加以下行,我添加了,但我再次收到错误
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>http://192.168.1.105:2300</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSRequiresCertificateTransparency</key>
<true/>
</dict>
</dict>
</dict>
为什么?
【问题讨论】:
-
your_domain_here,你换了吗? -
试试这个
<key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> -
@anhtu : 添加但又报错
-
尝试只向
NSAppTransportSecurity添加一个名为NSAllowsArbitraryLoads的布尔变量并将其设置为YES。 (虽然这是一种变通方法,并且您使用的方法是正确的方法)
标签: ios objective-c iphone xcode