我知道有一些类似的问题,我已经坚持了几天。我有一个项目,我使用ASIHTTPRequest对REST服务器执行POST和GET请求。一切正常,但由于ASIHTTPRequest开发已经停止,我计划将所有请求迁移到AFNetworking。实际上,我正在尝试它但是读取服务器响应似乎没有读取其中一个参数(GST)。
ASIHTTPRequest(工作正常):
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:[[NSString stringWithFormat:@"%sexteriorroute/listall", KServerUrl] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
[request addRequestHeader:@"Content-Type" value:@"application/json;charset=UTF-8"];
[request addRequestHeader:@"Cookie" value:[NSString stringWithFormat:@"GST=%@", [user getToken]]];
[request setRequestMethod:@"POST"];
[request setPostBody:[NSMutableData dataWithData:[[NSString stringWithFormat:@"{\"filters\":\"{\\\"type\\\":\\\"FastFilter\\\",\\\"groups\\\":[{\\\"groupOp\\\":\\\"AND\\\",\\\"rules\\\":[{\\\"field\\\":\\\"lang\\\",\\\"op\\\":\\\"eq\\\",\\\"data\\\":\\\"%@\\\"}]}],\\\"rules\\\":[],\\\"groupOp\\\":\\\"AND\\\"}\"}", [user getLang]] dataUsingEncoding:NSUTF8StringEncoding]]];
[request setDelegate:self];
[request setDidFailSelector:@selector(getExteriorRoutesPreviewFailed:)];
[request setDidFinishSelector:@selector(getExteriorRoutesPreviewFinished:)];
[request startAsynchronous];
AFNetworking请求(不起作用):
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:[NSString stringWithFormat:@"%sexteriorroute/listall", KServerUrl] parameters:[NSDictionary dictionaryWithObject:[user getToken] forKey:@"GST"] error:nil];
[request setHTTPShouldHandleCookies:NO];
[request setValue:[NSString stringWithFormat:@"GST=%@", [user getToken]] forHTTPHeaderField:@"Cookie"];
NSData *postBody = [self base64DataFromString:[NSString stringWithFormat:@"{\"filters\":\"{\\\"type\\\":\\\"FastFilter\\\",\\\"groups\\\":[{\\\"groupOp\\\":\\\"AND\\\",\\\"rules\\\":[{\\\"field\\\":\\\"lang\\\",\\\"op\\\":\\\"eq\\\",\\\"data\\\":\\\"%@\\\"}]}],\\\"rules\\\":[],\\\"groupOp\\\":\\\"AND\\\"}\"}", [user getLang]]];
[request addValue:@"application/json;UTF-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:[NSString stringWithFormat:@"%@",[user getToken]] forHTTPHeaderField:@"GST"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postBody];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", operation.responseString);
}];
[operation start];
答案 0 :(得分:0)
你试过这个吗?
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"foo": @"bar"};
[manager POST:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
答案 1 :(得分:0)
好吧,最后我明白了。这是与AFNetworking和ASIHTTP混合相关的问题。 AFNetworking请求本身很好。
似乎在使用ASIHTTP请求登录后端系统后,使用AFNetworking的请求无效。不知道为什么,但只使用AFNetworking请求解决了这个问题。
感谢大家=)