我是 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 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);
}];
}
我已将此代码链接到调用此函数的简单按钮。
我有另一个应用程序,在 ruby motion 中,使用此功能可以正常工作,我可以获得响应而不会出现任何错误。但是使用这个简单的应用程序我无法执行任何请求,它们都返回了这个错误代码 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}
所以我真的很想知道我做错了什么,有人可以帮助我吗? 谢谢
编辑:
这是在属性(property)中拯救经理的好方法还是我做错了什么? 如果这是好方法,这似乎行不通 感谢您的帮助
.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);
}];
最佳答案
在我的案例中,iOS 10 SDK 导致 AFNetworking 错误代码 -999。如果您尝试访问具有 SSL 的服务器并且您不想检查它,请向 Afnetworking 添加一些隐私政策
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
securityPolicy.allowInvalidCertificates = YES;
[securityPolicy setValidatesDomainName:NO];
关于ios - 请求错误代码为 999 的 AFNetworking,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33146003/