Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
755 views
in Technique[技术] by (71.8m points)

ios - AFNetworking with request error code 999

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);
}];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

[securityPolicy setValidatesDomainName:NO];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...