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

时间:2014-08-09 05:48:46

标签: ios objective-c post nsurlrequest afnetworking-2

我尝试使用AFNetworking对http正文进行POST请求。我使用this post's回答

  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];

工作并给我正确的数据。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

将断点添加到NSURL * url = [NSURL URLWithString:urlString];并检查如果你得到url null你得到正确的网址然后尝试下面的代码

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