AFNetworking POST返回999错误

时间:2017-09-20 22:04:51

标签: ios objective-c afnetworking

在我的应用中,我正在对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;

我也尝试过更改传输安全性。这两种解决方案都没有做任何事关于什么可能出错的任何想法?

1 个答案:

答案 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>

https://i.stack.imgur.com/7xNAC.png