2

我已经开始使用 AFNetworking,它在制作简单的“GET”请求时效果很好。但是现在我正在尝试执行“POST”请求。我使用下面的代码来执行“GET”请求。查看AFHTTPClientputhPath时,无法设置用于正文的数据。我的猜测是还有另一种方法可以解决这个问题。我一直在将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
        }
 ];
4

3 回答 3

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;
}
于 2012-01-26T06:48:29.820 回答
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 应用程序中。

于 2015-03-23T05:20:44.567 回答
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 和一般 RESTful :-)

于 2013-09-23T20:30:24.557 回答