AFNetworking 2 - 发布请求 - 向正文添加简单文本

时间:2014-01-08 22:53:07

标签: ios objective-c afnetworking-2

这是我在这里发表的第一篇文章并不是重新发布,我在Stack Overflow中搜索了很多答案,但没有运气......我遇到的问题是间歇性错误:“请求正文流用尽了代码:-1021,我正在使用AFNetworking 2。

这是我用来创建RequestOperationManager:

 - (AFHTTPRequestOperationManager *)createDefaultManager {

     AFHTTPRequestOperationManager *postRequest = [[AFHTTPRequestOperationManager alloc]initWithBaseURL:[NSURL URLWithString:@"myURL"]];
     postRequest.securityPolicy.allowInvalidCertificates = YES;
     postRequest.responseSerializer = [AFXMLParserResponseSerializer serializer];
     [postRequest.requestSerializer setValue:@"Keep-Alive" forHTTPHeaderField:@"Connection"];
     [postRequest.requestSerializer setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
     [postRequest.requestSerializer setValue:@"en-US" forHTTPHeaderField:@"Content-Language"];

     return postRequest;
 }

这是我的帖子方法:

 - (void)postRequest:(AFHTTPRequestOperationManager *)request withURL:(NSString *)url withBody:(NSString *)body {

    [request POST:url parameters:nil constructingBodyWithBlock:^(id <AFMultipartFormData>formData)
    {
       [formData throttleBandwidthWithPacketSize:kAFUploadStream3GSuggestedPacketSize
                                                  delay:kAFUploadStream3GSuggestedDelay];

       NSMutableDictionary *mutableHeaders = [NSMutableDictionary dictionary];
       [mutableHeaders setValue:[NSString stringWithFormat:@"text/xml"] forKey:@"Content-Type"];
       [mutableHeaders setValue:[NSString stringWithFormat:@"any-value"] forKey:@"User-Agent"];
       [formData appendPartWithHeaders:mutableHeaders body:[NSData dataWithBytes:[body UTF8String] length:[body length]]];

     } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self connectionSuccess:operation withResponse:responseObject];
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self connectionFailed:operation withError:error];
        NSLog(@"Here's the request: %@", operation.request.HTTPBody);
 }];
}

我不知道我在做什么有什么问题。这应该一直有效。

谢谢, JJ

0 个答案:

没有答案