在我的应用中,我正在对API进行POST
。在这种情况下,我将基本网址设置为https://jsonplaceholder.typicode.com/
。然后我执行下面的代码:
[self POST:@"posts"
parameters:nil
constructingBodyWithBlock:nil
success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"response: %@", responseObject);
success(responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"Error: %@", error);
failure(error);
}];
运行此代码时,会记录错误。
Error: Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLStringKey=https://jsonplaceholder.typicode.com/posts, NSLocalizedDescription=cancelled, NSErrorFailingURLKey=https://jsonplaceholder.typicode.com/posts}
我尝试更改安全策略。
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
securityPolicy.allowInvalidCertificates = YES;
[securityPolicy setValidatesDomainName:NO];
self.securityPolicy = securityPolicy;
我也尝试过更改传输安全性。这两种解决方案都没有做任何事关于什么可能出错的任何想法?
答案 0 :(得分:-2)
在info.plist
:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>example.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>