在AFNetworking 1X JSON请求上设置http标头

时间:2014-06-05 19:42:57

标签: ios json http-headers afnetworking

我将参数传递给REST Web服务,它一直在说:

The server refused this request because the request entity is in a format 
not supported by the requested resource for the requested method

我使用我的所有参数创建这样的NSDictionary:

    NSMutableDictionary *webServiceParameters;
    webServiceParameters = [[NSMutableDictionary alloc] init];
    [webServiceParameters setObject:mytextbox.text forKey:@"value1"];

然后我将其传递给请求:

 NSURL *url = [NSURL URLWithString:webServiceURL];
        AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:url];
               NSURLRequest *myRequest = [client requestWithMethod:@"POST" path:@"" parameters:webServiceParameters];
        client.parameterEncoding = AFJSONParameterEncoding;

        NSLog(@"%@", myRequest);

    AFJSONRequestOperation *operation;
    operation =  [AFJSONRequestOperation JSONRequestOperationWithRequest:myRequest

                   success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSDictionary *JSON)
                  {


                  }



                   failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSArray *JSON)
                  {
                      NSLog(@"%@", [error userInfo]);
                  }
                  ];

    [operation start];

我的假设是:

client.parameterEncoding = AFJSONParameterEncoding;

会将参数转换为JSON请求,但不会更改头文件。我无法找到改变头文件的方法。关于AFNetworking的所有文档现在都在2.0中,我仍然需要使用1.X ..我如何在头文件中传递一个值来告诉它它的application / json,并且该参数是否会将它实际作为JSON传递给它?

我尝试过:[myRequest setValue:@"" forHTTPHeaderField:@""];但编译器会抛出此错误:

No visible interface for NSUrlRequest declares the selector setValue: for HTTPHeaderField

0 个答案:

没有答案