AFNetworking 2 POST与身份验证挑战

时间:2014-09-02 00:49:02

标签: afnetworking

我正在使用AFNetworking 2,AFHTTPRequestOperation我可以使用我的Get

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"GET"];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.securityPolicy = securityPolicy;
    [operation setWillSendRequestForAuthenticationChallengeBlock:
     ^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) {
    //the certificate
    }
 [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        DLog(@"operation :: %@", responseObject);
        NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        DLog(@"operation :: %@", result);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        DLog(@"operation error :: %@", error);
    }];
    [operation start];

但现在我需要使用带参数的POST

我在找到如何在

上设置参数时遇到问题
  

AFHTTPRequestOperation

或找到如何设置挑战块

  

AFHTTPRequestOperationManager

如何使用参数和挑战块进行POST?

欢呼声

1 个答案:

答案 0 :(得分:1)

我现在正在处理POST请求。到目前为止,我在尝试使用POST方法发送NSDictionary时想出了以下代码:

    NSDictionary*packet = [NSDictionary dictionaryWithObjectsAndKeys: ......

    [manager POST:path
       parameters:packet
          success:^(AFHTTPRequestOperation *operation, id responseObject) {
                 NSLog(@"JSON: %@", responseObject);
                 if ([responseObject isKindOfClass:[NSDictionary class]])
                     [self parseReceivedDataPacket:responseObject];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                 NSLog(@"Error: %@", error);
     }];

实际上它对我有用,除了在发送这些数据时得到“不可接受的内容类型:text / html”。但它收到了。

希望这很有用。