我对AFNetworking 2.0有疑问
我想在我的iOS应用中集成AFNetworking 2.0。在集成之前,我使用这样的代码来发出POST请求:
NSString *string = [self.serverName stringByAppendingString:resource];
NSURL *url = [NSURL URLWithString:string];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setRequestMethod:@"POST"];
[request setUsername:username];
[request setPassword:password];
if(data != nil)
{
[request setPostBody:data];
[request addRequestHeader:@"Content-Type" value:@"application/json"];
}
[request setCompletionBlock:^{
....
}];
[request setFailedBlock:^{
.....
}];
[request startAsynchronous];
在AFNetworking Integration中,我以这种方式发出POST请求:
我使用单例类:AFHTTPSessionManager。 我设置:
self.responseSerializer = [AFJSONResponseSerializer serializer];
self.requestSerializer = [AFJSONRequestSerializer serializer];
POST请求:
NSMutableDictionary *requestParameters = [NSMutableDictionary dictionary];
[requestParameters setObject:@"....." forKey:@"....."];
[self POST:@"/resource" parameters:requestParameters success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"Ok");
}
failure:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"Failed");
}];
我有相同的参数和网址。
但是第二种方式请求失败了。我有一个错误:请求失败:不可接受(406)
这两种方式是相同的吗?首先,我将参数(在NSData中转换)设置为请求体。这是我的第二种方式吗?