将 POST 请求从 AsiHTTPRequest 翻译成 AFNetworking

Translate POST request from AsiHTTPRequest to AFNetworking

我知道有一些类似的问题,我已经在这个问题上卡了几天了。我有一个项目,我在其中使用 ASIHTTPRequest 对 REST 服务器执行 POST 和 GET 请求。一切正常,但由于 ASIHTTPRequest 开发已停止,我计划将所有请求迁移到 AFNetworking。事实上,我正在尝试,但读取服务器响应似乎没有读取其中一个参数 (GST)。

A​​SIHTTPRequest(工作正常):

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];

A​​FNetworking 请求(无效):

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];

你试过这个吗

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);
}];

好吧,我终于明白了。这是一个与混合 AFNetworking 和 ASIHTTP 有关的问题。 AFNetworking 请求本身没有问题。

似乎在使用 ASIHTTP 请求登录后端系统后,使用 AFNetworking 的请求无法正常工作。不知道真正的原因,但仅使用 AFNetworking 请求解决了问题。

感谢大家=)