use*_*991 19 iphone objective-c ipad ios afnetworking
我正在使用AFHTTPRequestOperationManager(2.0 AFNetworking库)来发送REST POST请求.但是经理只有调用来设置参数.
-((AFHTTPRequestOperation *)POST:(NSString *)URLString
parameters:(NSDictionary *)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
Run Code Online (Sandbox Code Playgroud)
我还需要用字符串设置HTTP请求体.我怎么能用AFHTTPRequestOperationManager来做呢?谢谢.
Gan*_*uri 31
我遇到了同样的问题并通过添加代码解决了它,如下所示:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL
cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
[request setHTTPMethod:@"POST"];
[request setValue:@"Basic: someValue" forHTTPHeaderField:@"Authorization"];
[request setValue: @"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody: [body dataUsingEncoding:NSUTF8StringEncoding]];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON responseObject: %@ ",responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", [error localizedDescription]);
}];
[op start];
Run Code Online (Sandbox Code Playgroud)
小智 7
对于AFHTTPRequestOperationManager
[requestOperationManager.requestSerializer setValue:@"your Content Type" forHTTPHeaderField:@"Content-Type"];
[requestOperationManager.requestSerializer setValue:@"no-cache" forHTTPHeaderField:@"Cache-Control"];
// Fill parameters
NSDictionary *parameters = @{@"name" : @"John",
@"lastName" : @"McClane"};
// Customizing serialization. Be careful, not work without parametersDictionary
[requestOperationManager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, NSDictionary *parameters, NSError *__autoreleasing *error) {
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:nil];
NSString *argString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
return argString;
}];
[requestOperationManager POST:urlString parameters:parameters timeoutInterval:kRequestTimeoutInterval success:^(AFHTTPRequestOperation *operation, id responseObject) {
if (success)
success(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure)
failure(error);
}];
Run Code Online (Sandbox Code Playgroud)
检查那个方便方法(POST:参数:成功:失败)是在做什么,并自己动手来获取对实际NSMutableRequest对象的访问权限.
我正在使用AFHTTPSessionManager而不是AFHTTPRequestOperation但我想这个机制是类似的.
这是我的解决方案:
设置会话管理器(标题等)
手动创建NSMutable请求并添加我的HTTPBody,基本上是在该方便方法中复制粘贴代码.看起来像这样:
NSMutableURLRequest *request = [manager.requestSerializer requestWithMethod:@"POST" URLString:[[NSURL URLWithString:<url string>] absoluteString] parameters:parameters];
[request setHTTPBody:[self.POSTHttpBody dataUsingEncoding:NSUTF8StringEncoding]];
__block NSURLSessionDataTask *task = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {
if (error) {
// error handling
} else {
// success
}
}];
[task resume];
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
31194 次 |
| 最近记录: |