ios 请求错误代码为 999 的 AFNetworking

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33146003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:00:11  来源:igfitidea点击:

AFNetworking with request error code 999

iosobjective-cafnetworkingrubymotion

提问by xikimay

I'm new to objective-c and i'm having a hard time with a AFNetworking.

我是objective-c的新手,我在使用AFNetworking时遇到了困难。

So the thing is that i want to make a simple POST request to a server who will send me back a salt. I'v make a simple app, in order to test my request but i don't understand why i'm getting the error code 999.

所以问题是我想向服务器发出一个简单的 POST 请求,该服务器将向我发回盐。我制作了一个简单的应用程序,以测试我的请求,但我不明白为什么我会收到错误代码 999。

Here a sample of my code.

这是我的代码示例。

+ (void)simpleRequest; {
    NSURL *mailserver = [NSURL URLWithString:@"https://localhost:4443/"];

    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithBaseURL:mailserver];
    manager.securityPolicy.allowInvalidCertificates = TRUE;
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    NSDictionary *parameters = @{@"username": @"testtest"};

    [manager POST:@"api/v0/login/salt" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

I' have link this code to a simple Button which's calling this function.

我已将此代码链接到一个调用此函数的简单按钮。

I have an other app, in ruby motion which's work fined with this function i can get the response without any error. But with this simple app i can't do any request, they all returned this error code 999.

我有另一个应用程序,在 ruby​​ 运动中使用此功能可以正常工作,我可以获得响应而没有任何错误。但是使用这个简单的应用程序我不能做任何请求,他们都返回了这个错误代码 999。

Error: Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLKey=https://localhost:4443/api/v0/login/salt, NSLocalizedDescription=cancelled, NSErrorFailingURLStringKey=https://localhost:4443/api/v0/login/salt}

错误:错误域=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLKey= https://localhost:4443/api/v0/login/salt, NSLocalizedDescription=cancelled, NSErrorFailingURLStringKey= https://localhost:4443/api/ v0/登录/盐}

So i'm really wondering what i'm doing wrong, anyone can help me on this ? Thanks

所以我真的想知道我做错了什么,任何人都可以帮助我吗?谢谢

EDIT:

编辑:

Is it the good way for saving the manager in a property or am i doing something wrong ? If it's the good way, this seems to not work Thanks for the help

这是将经理保存在物业中的好方法还是我做错了什么?如果这是好方法,这似乎不起作用谢谢您的帮助

.h file

.h 文件

@property (nonatomic, retain) AFHTTPSessionManager *session;

.m file

.m 文件

@synthesize session;
- (IBAction)log:(id)sender {
NSURL *mailserver = [NSURL URLWithString:@"https://localhost:4443/"];

self.session = [[AFHTTPSessionManager alloc]initWithBaseURL:mailserver];
self.session.securityPolicy.allowInvalidCertificates = TRUE;
self.session.responseSerializer = [AFJSONResponseSerializer serializer];
self.session.requestSerializer = [AFJSONRequestSerializer serializer];
NSDictionary *parameters = @{@"username": @"testtest"};

[self.session POST:@"api/v0/login/salt" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(NSURLSessionDataTask *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

采纳答案by EFE

In my case iOS 9 SDK's "App transport security" cause AFNetworkingerror code : -999. If you're trying to reach a server that doesn't have a SSL add keys like screenshot below.

就我而言,iOS 9 SDK 的“应用程序传输安全性”导致AFNetworking错误代码:-999。如果您尝试访问没有 SSL 的服务器,请添加如下屏幕截图所示的密钥。

enter image description here

在此处输入图片说明

回答by LittleBoat

In my case iOS 10 SDK's caused AFNetworking error code -999. If you're trying to reach a server that has SSL and you don't want to check it out, add some privacy Policy to Afnetworking

就我而言,iOS 10 SDK 导致 AFNetworking 错误代码 -999。如果您尝试访问具有 SSL 的服务器并且不想查看它,请向 Afnetworking 添加一些隐私政策

AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
securityPolicy.allowInvalidCertificates = YES;

[securityPolicy setValidatesDomainName:NO];

回答by Ewan Mellor

That's error -999, not 999. That is NSURLErrorCancelled. Your request has been cancelled before it can be completed.

那是错误 -999,而不是 999。那是NSURLErrorCancelled. 您的请求在完成之前已被取消。

Looking at your code, you aren't retaining the AFHTTPSessionManager *manageranywhere. This means that the manager will be disposed as soon as +simpleRequestreturns. I'm guessing that this is what is cancelling your request.

查看您的代码,您不会保留AFHTTPSessionManager *manager任何地方。这意味着经理将在+simpleRequest返回后立即处理。我猜这就是取消您的请求的原因。

You need to save the manager so that it lives for the full duration of the request. Save it in a property somewhere.

您需要保存管理器,使其在整个请求期间都有效。将其保存在某处的属性中。

回答by CAHbl463

I noticed that your API endpoint indicates to a secure connection:

我注意到您的 API 端点指示安全连接:

httpS://localhost:4443/api/v0/login/salt

http S://localhost:4443/api/v0/login/salt

Just try it just in case, maybe it repeats your situation.

只是为了以防万一,也许它会重复你的情况。

In my case, this was a typoin the API manager code. Which from the part can be said is connected with App Transport Security Settings.

就我而言,这是API 管理器代码中的一个错字。从这部分可以说是与App传输安全设置有关。

Just changed the protected protocol from httpS://to http://and the error:

刚刚将受保护的协议从httpS://tohttp://和错误更改为:

NSURLErrorDomain Code = -999 "cancelled"

NSURLErrorDomain 代码 = -999 “已取消”

was gone and it all worked!

不见了,一切都奏效了

+And also if you had a similar problem. Be sure to discuss this with a backend specialistwho deals with the server or API configuration for your application. This means that the server does not have valid security certificates. Perhaps you still need a secure connection. Or this specialist can again configure everything back from http://to httpS://, and I'm not sure (did not check) whether this will work again when in the code you are already using a non-secure http://connection.

+如果你有类似的问题。请务必负责为您的应用程序处理服务器或 API 配置的后端专家讨论此问题。这意味着服务器没有有效的安全证书。也许您仍然需要安全连接。或者该专家可以再次配置从http://到 的所有内容httpS://,并且我不确定(没有检查)当您在代码中已经使用非安全http://连接时这是否会再次工作。

回答by Vyrnach

[Original OP question may not mention Cordova but this link was very high in the search result when Googling this issue]

[原 OP 问题可能没有提到 Cordova 但在谷歌搜索这个问题时,这个链接在搜索结果中非常高]

For my Cordova project (or similar), turns out it was a plugin issue. Make sure you're not missing any plugins and make sure they're installed properly without issue.

对于我的Cordova 项目(或类似项目),结果是插件问题。确保您没有丢失任何插件,并确保它们安装正确而没有问题。

Easiest way to verify this is simply to start fresh by recreating the Cordova project(cordova create <path>) along with the required platforms (cordova platform add <platform name>) and add each plugin with the verbose flag (--verbose) so that you can see if anything went wrong in the console log while the plugin is being downloaded, added to project and installed for each platform (cordova plugin add cordova-plugin-device --verbose)

验证这一点的最简单方法是通过重新创建 Cordova 项目( cordova create <path>) 以及所需的平台 ( cordova platform add <platform name>) 重新开始,并使用详细标志 (--verbose) 添加每个插件,以便您可以查看控制台日志中是否出现任何问题在下载插件、添加到项目并为每个平台安装时 ( cordova plugin add cordova-plugin-device --verbose)

Recap: cordova create <path>cordova platform add <platform name>cordova plugin add cordova-plugin-device --verbose

回顾: cordova create <path>cordova platform add <platform name>cordova plugin add cordova-plugin-device --verbose