繁体   English   中英

无法在AFNetworking POST请求上设置HTTP正文

[英]Unable to set HTTP body on AFNetworking POST request

我正在尝试使用AFNetworking与http正文进行POST请求。 我用这个帖子的答案

  NSString *urlString = [NSString stringWithFormat:@"my-url"];
  NSString *parameterData = [NSString stringWithFormat:@"request_type=get_story_nids&story_type=%@&story_number=%@", section, count];

  NSURL *url = [NSURL URLWithString:urlString];
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  [request setHTTPMethod:@"POST"];
  [request setHTTPBody:[parameterData dataUsingEncoding:NSUTF8StringEncoding]];
  [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
  [request setValue:@"application/json" forHTTPHeaderField:@"content-type"];
  AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        op.responseSerializer = [AFJSONResponseSerializer serializer];
        [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id result) {
              completionBlock(nids,nil);  
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            completionBlock(nil, error);
        }];

我在op setCompletionBlockWithSuccess的完成块中放置了断点,它甚至没有击中它。

如果有帮助,那就做

NSMutableData* result = [[NSURLConnection sendSynchronousRequest:request   returningResponse:&response error:&error] mutableCopy];

可以给我正确的数据。

有任何想法吗?

将断点添加到NSURL * url = [NSURL URLWithString:urlString]; 并检查是否获得正确的URL(如果您获得的URL为空),然后尝试以下代码

NSURL *url = [[NSURL alloc] initWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSLog(@"url = %@",url); 

我认为您错过了将您的操作添加到操作队列的操作。 您可以在此处找到示例: http : //samwize.com/2013/03/02/queue-http-operations-with-afnetworking/

// Add the operation to a queue
// It will start once added
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
[operationQueue addOperation:op];

您还可以添加https://github.com/AFNetworking/AFHTTPRequestOperationLogger来记录AFNetworking正在处理的所有操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM