【发布时间】:2016-01-13 18:17:03
【问题描述】:
我是 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 运动中,使用此功能可以正常工作,我可以得到响应而没有任何错误。但是用这个简单的应用程序我不能做任何请求,他们都返回了这个错误代码 999。
错误:错误域=NSURLErrorDomain 代码=-999 “已取消” UserInfo={NSErrorFailingURLKey=https://localhost:4443/api/v0/login/salt, NSLocalizedDescription=取消, NSErrorFailingURLStringKey=https://localhost:4443/api/v0/login/salt}
所以我真的想知道我做错了什么,任何人都可以帮助我吗? 谢谢
编辑:
这是将经理保存在财产中的好方法还是我做错了什么? 如果这是好方法,这似乎行不通 感谢您的帮助
.h 文件
@property (nonatomic, retain) AFHTTPSessionManager *session;
.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);
}];
【问题讨论】:
-
这表明你的 URL 是错误的......也许应该将 localhost 转换为你的 IP 地址?
-
有时参数会在请求正文中,而参数字段为零:比如https://localhost:4443/api/v0/login/salt?username=testtest
-
我刚试了下还是不行,至少感谢帮助
-
在我的情况下,它应该是像“localhost:4443/api/v0/login/salt”这样的请求,这在我的其他应用程序中可以正常工作
-
我在 Google 中进行了一些搜索,他们说这是因为前一个请求尚未完成而另一个请求已发送,因此您收到 Canceled 错误
标签: ios objective-c afnetworking rubymotion