我需要在发出POST请求时在URL中发送查询字符串以及正文中的JSON。要在网址中发送查询字符串,我会按this thread on SO中的建议覆盖HTTPMethodsEncodingParametersInURI
属性
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:fullUrl]];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer.HTTPMethodsEncodingParametersInURI = [NSSet setWithArray:@[@"POST", @"GET", @"HEAD", @"PUT", @"DELETE"]];
[manager POST:fullUrl parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
但似乎这样做也会将我的JSON数据添加到URL中,因此我的请求不会在服务器上执行。但我只需要发送'命令'参数进入url,一些JSON数据进入请求体。
我的fullUrl
字符串看起来像http://some_ip_address/config?command=some_command,而param是NSDictionary
对象。
请注意fullUrl
中有一个参数,即命令,我还会在
NSDictionary
对象 param
[manager POST:fullUrl parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
Edit2:我也试过了NSURLSession
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:nil];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://my_server_ip/config?command=plugs"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSError *error;
NSData *postData = [NSJSONSerialization dataWithJSONObject:param options:0 error:&error];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}];
[postDataTask resume];
但获取状态代码:NSURLResponse中的400。
编辑3:有人建议在this SO thread中继承AFHTTPRequestSerializer
类,所以我尝试了
@implementation CustomAFHTTPRequestSerializer
-(id)init
{
self = [super init];
return self;
}
- (NSMutableURLRequest *)requestWithMethod:(NSString *)method
URLString:(NSString *)URLString
parameters:(NSDictionary *)parameters
error:(NSError *__autoreleasing *)error
{
NSString* encodedUrl = [URLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:encodedUrl] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSError *error1;
NSData *postData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:&error1];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
return request;
}
@end
并将其指定为operationManager的requestSerializer
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [[CustomAFHTTPRequestSerializer alloc] init];
但仍然得到Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: bad request (400)
请帮助我们,非常感谢任何帮助。