AFNetworking with request error code 999

I'm new to objective-c, and it's hard for me to spend time with AFNetworking.

So, I want to make a simple POST request to the server, which will send me salt. I am making a simple application to check my request, but I do not understand why I am getting error code 999.

Here is an example 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 associate this code with a simple button that calls this function.

I have another application, in ruby ​​movement, which works with this function, I can get an answer without any errors. But with this simple application, I can’t fulfill any request, they all returned this error code 999.

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

So I'm really curious what I'm doing wrong, can anyone help me with this? Thanks

EDIT:

Is this a good way to keep the manager in the property, or am I doing something wrong? If this is a good way, this doesn't seem to 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); }]; 
+12
ios objective-c afnetworking rubymotion
Oct 15 '15 at 10:24
source share
4 answers

In my case, the iOS 9 SDK β€œ Vehicle Security Application ] causes AFNetworking error code: -999 . If you try to contact a server that does not have keys for adding SSL, such as the screenshot below.

enter image description here

+2
Apr 16 '16 at 11:35
source share

In my case, the iOS 10 SDK raises an AFNetworking error code: -999. If you are trying to connect to a server that has SSL and you do not want to check it, add some privacy policy in Afnetworking

 AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone]; securityPolicy.allowInvalidCertificates = YES; [securityPolicy setValidatesDomainName:NO]; 
+19
Oct 18 '16 at 5:27
source share

This error is -999, not 999. This is NSURLErrorCancelled . Your request has been canceled before it is completed.

AFHTTPSessionManager *manager looking at your code, you are not saving the AFHTTPSessionManager *manager anywhere. This means that the manager will be deleted as soon as +simpleRequest returns. I assume this is what cancels your request.

You need to keep the manager so that it works for the entire duration of the request. Save it in any property.

+10
Oct. 15 '15 at 17:08
source share

I noticed that the API endpoint indicates a secure connection:

HTTP S : // local: 4443 / API / v0 / login / salt

Just try, just in case, maybe this will repeat your situation.

In my case, it was a typo in the API manager code. Which of the parts can be said to be related to application transport security settings.

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

Code NSURLErrorDomain = -999 "canceled"

was not and it all worked !

+ And if you had a similar problem. Be sure to discuss this with the support specialist who is setting up the server or API for your application. This means that the server does not have valid security certificates. You may still need a secure connection. Or, this specialist can configure everything from http:// to httpS:// , and I'm not sure (did not check) whether this will work again when you are already using an insecure http:// connection in the code.

0
Dec 01 '17 at 2:02 on
source share



All Articles