2

使用 iOS,我正在尝试与请求 3 个标头后跟 JSON POST 数据的 Web 服务进行通信。

我查看了以下 AFNetworking 片段,它将字典转换为 JSON 文件。在这种情况下,我正在尝试发布标头和 JSON 文件。如果您有任何建议,请告诉我:

NSURL *url = [NSURL URLWithString:WalletKit_URL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
httpClient.parameterEncoding = AFJSONParameterEncoding;
NSDictionary *params = @{@"brand-id" : Brand_Id, @"api-key" : API_Key, @"Content-Type" : @"application/json"};
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"" parameters:params];
AFHTTPRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSURLResponse *response, id JSON) {
    NSLog(@"success");
} failure:^(NSURLRequest *request, NSURLResponse *response, NSError *error, id JSON) {
    NSLog(@"error");
}];
4

1 回答 1

4

您应该将 HTTP 标头字段添加到NSMutableURLRequest.

[request addValue:@"foobar" forHTTPHeaderField:@"X-Foo-Bar"];
于 2013-02-05T19:32:28.550 回答