AFNetworking POST 请求失败代码=-1011 (400)
Posted
技术标签:
【中文标题】AFNetworking POST 请求失败代码=-1011 (400)【英文标题】:AFNetworking POST Request failed Code=-1011 (400) 【发布时间】:2015-05-31 15:55:02 【问题描述】:我有一个网络服务,我可以通过 Postman 实用程序成功地对其进行 post 调用。在 Postman 上,设置为
我使用 AFNetworking。这是我的代码:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @@"offset": @"1", @"csrf-token": @"63b164ea557b4aba694f81520c71972fa43a9772";
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager POST:@"http://simple.com/" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject)
NSLog(@"JSON: %@", responseObject);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"Error: %@", error);
];
但我收到此错误:
Error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo=0x7a6c5240 com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7a6b5360> URL: http://simple.com/ status code: 400, headers
"Cache-Control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0, private, must-revalidate";
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Type" = "text/html; charset=utf-8";
Date = "Sun, 31 May 2015 15:40:26 GMT";
Expires = "Thu, 19 Nov 1981 08:52:00 GMT";
Pragma = "no-cache";
Server = "nginx/1.6.2";
"Set-Cookie" = "csrf-token=5589461e87b28a538953ccac4168bf1f98b20b1ds%3A40%3A%22f52bda5262260a3ee0a10efcf48a6a49c11e4103%22%3B; path=/, phpSESSID=803db60816b9f8203fb8feca01c130b6; path=/";
"Transfer-Encoding" = Identity;
Vary = "Accept-Encoding,User-Agent";
"X-Powered-By" = "PHP/5.5.17";
这没有 AFNetworking:
NSString *post = @"offset=1&csrf-token=63b164ea557b4aba694f81520c71972fa43a9772";
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://simple.com/"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded; charset=UTF-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"~~~~~ Status code: %d", [response statusCode]);
又是一个错误:400
请帮忙解决问题
【问题讨论】:
@ilesh 更正我在下面的答案中描述的问题 【参考方案1】:我解决了他的问题。
服务器使用特定的 CSRF 令牌 保持会话的事实。我的错误是我在浏览器和移动设备中使用了相同的令牌。因此,服务器并没有详细说明获取错误的 POST 请求。
我的解决方案:我首先获得一个服务器 CSRF 令牌,然后对于进一步的POST
查询,我使用生成的令牌。作为对 POST 请求的响应,我得到一个状态 200 和我需要的数据。
True AFNetworking
我并没有真正帮助解决这个问题,所以我不得不使用标准NSURLConnection
(我把代码放在问题中)。也许将来有人会派上用场!
谢谢大家!祝大家好运!
【讨论】:
以上是关于AFNetworking POST 请求失败代码=-1011 (400)的主要内容,如果未能解决你的问题,请参考以下文章
带有 AFNetworking 的 iOS POST:NSLocalizedDescription=请求失败:内部服务器错误 (500)
AFNetworking 将 NSMutableDictionary 作为 POST 发送
AFNetworking 问题:NSErrorFailingURLKey=url,NSLocalizedDescription=请求失败:错误请求 (400)