我已经开始使用AFNetworking,它可以很好地进行简单的“GET”请求。但是现在我正在尝试进行“POST” - 请求。我使用下面的代码来执行“GET”请求。查看 AFHTTPClient 的 puthPath 时,无法设置要用于正文的数据。我的猜测是,有另一种解决方法。我一直在寻找 AFHTTPOperation 来解决这个问题。但是,我没有让这个工作。问题是我不知道如何在基本身份验证中使用它。
有人可以告诉我如何使用AFNetworking进行简单的“POST”请求吗?
AFHTTPClient* client = [AFHTTPClient clientWithBaseURL:ServerURL];
[client setAuthorizationHeaderWithUsername:self.username
password:self.password];
NSString* resourcePath = [NSString stringWithFormat:@"/some/resource/%@",
endPath];
[client getPath:resourcePath
parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
// Success code omitted
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// Some error handling code omitted
}
];
答案 0 :(得分:13)
我没有找到任何简单的方法来做到这一点。但我按照建议做了并创建了我自己的 AFHTTPClient 子类。在子类中,我实现了以下方法。这使得可以执行POST请求和&使用我自己的数据进行PUT请求。
- (void)postPath:(NSString *)path
parameters:(NSDictionary *)parameters
data:(NSData*)data
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
{
NSURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters data:data];
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
[self enqueueHTTPRequestOperation:operation];
}
- (void)putPath:(NSString *)path
parameters:(NSDictionary *)parameters
data:(NSData*)data
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
{
NSURLRequest *request = [self requestWithMethod:@"PUT" path:path parameters:parameters data:data];
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];
[self enqueueHTTPRequestOperation:operation];
}
-(NSMutableURLRequest*)requestWithMethod:(NSString *)method
path:(NSString *)path
parameters:(NSDictionary *)parameters
data:(NSData*)data;
{
NSMutableURLRequest* request = [super requestWithMethod:method
path:path
parameters:parameters];
[request setHTTPBody:data];
return request;
}
答案 1 :(得分:3)
使用AFNetworking 2.0,我只需从
复制代码- (AFHTTPRequestOperation *)PUT:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
并添加
[request setHTTPBody:data];
就是这样:
NSString* str = [bookDetailLink objectForKey:@"Body"];
NSData* data = [str dataUsingEncoding: NSUTF8StringEncoding];
NSMutableURLRequest *request = [self.manager.requestSerializer requestWithMethod:@"PUT" URLString:bookingDetailUrl parameters:nil error:nil];
[request setHTTPBody:data];
AFHTTPRequestOperation *operation = [self.manager HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *op, NSHTTPURLResponse *response) {
NSLog(@"%@", response);
}
failure:^(AFHTTPRequestOperation *op, NSError *error) {
NSLog(@"%@", error);
}];
[self.manager.operationQueue addOperation:operation];
我正在使用AFNetworking将Skyscanner API集成到我们的iOS应用程序中。
答案 2 :(得分:0)
使用AFNetworking 1.3.2,以下代码适用于我:
NSData *imageData = UIImageJPEGRepresentation(thumb, 0.85F);
AFHTTPClient *httpClient = [[AFHTTPClient alloc]
initWithBaseURL:[NSURL URLWithString:@"https://example.com/"]];
NSMutableURLRequest *request = [httpClient
requestWithMethod:@"PUT" path:@"/foo" parameters:nil];
[request setHTTPBody:imageData];
[request setValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];
AFHTTPRequestOperation *operation = [httpClient
HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *op, NSHTTPURLResponse *response) {
NSLog(@"%@", response);
}
failure:^(AFHTTPRequestOperation *op, NSError *error) {
NSLog(@"%@", error);
}];
[operation start];
这导致PUT请求具有正确的标题,Content-Lenght和一般RESTfulness: - )