AFNetworking请求错误代码999 [英] AFNetworking with request error code 999
问题描述
我是Objective-c的新手,我在使用AFNetworking时遇到了困难。
所以我想做一个简单的POST请求服务员给我发回盐。
我做一个简单的应用程序,以测试我的请求,但我不明白为什么我得到错误代码999.
这里我的代码示例。
+(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序列化程序];
NSDictionary * parameters = @ {@username:@testtest};
[经理POST:@api / v0 / login / salt参数:参数成功:^(NSURLSessionDataTask *操作,id responseObject){
NSLog(@JSON:%@ ,responseObject);
}失败:^(NSURLSessionDataTask *操作,NSError *错误){
NSLog(@错误:%@,错误);
}];
}
我已将此代码链接到一个调用此函数的简单Button。 / p>
我还有一个其他的应用程序,在ruby运动中使用此功能进行罚款我可以得到响应而没有任何错误。但是使用这个简单的应用程序,我无法做任何请求,他们都返回此错误代码999。
错误:错误域= NSURLErrorDomain代码= -999已取消
UserInfo = {NSErrorFailingURLKey =I'm new to objective-c and i'm having a hard time with a 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.
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.
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}
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
@property (nonatomic, retain) AFHTTPSessionManager *session;
.m file
@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); }];
解决方案In my case iOS 9 SDK's "App transport security" cause AFNetworking error code : -999. If you're trying to reach a server that doesn't have a SSL add keys like screenshot below.
这篇关于AFNetworking请求错误代码999的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!