无法将响应头设置为application / json

时间:2013-12-06 14:44:28

标签: json rest

我的请求标题是application / json,但我的响应标题是application / xml。如何将响应标题更改为application / json?

请求标题:

"Accept-Encoding" = gzip;
"Content-Length" = 21;
"Content-Type" = "application/json";
"User-Agent" = "BarcodeReader 1.0 (iPhone Simulator; iPhone OS 7.0.3; en_US)";

响应标题:

"Cache-Control" = private;
Connection = close;
"Content-Length" = 689;
"Content-Type" = "application/xml; charset=utf-8";
Date = "Mon, 09 Dec 2013 06:45:32 GMT";
Server = "Microsoft-IIS/7.5";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";

代码:

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://wcftest.amerikanhastanesi.org/BarkodIOS.svc/rest/HastaAra"]];
    NSData *myPostData = [[NSString stringWithFormat:@"{\"protokolNo\":%d}",protokolNo] dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableData *myMutablePostData = [[NSMutableData alloc]initWithData:myPostData];

    [request setPostBody:myMutablePostData];
    [request setRequestMethod:@"POST"];

    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    [request setDelegate:self];
    [request startSynchronous];
    NSLog(@"%@",[request responseHeaders]);
    NSLog(@"%@",[request requestHeaders]);

     id jsonObjects = [NSJSONSerialization JSONObjectWithData:[[request responseString]
     dataUsingEncoding:NSUTF8StringEncoding]
     options:NSJSONReadingMutableContainers
     error:nil ];
    HastaModel *h;
    for(NSDictionary *theItem in [jsonObjects objectForKey:@"HastaAraResult"]){
        h = [[HastaModel alloc]initWithDictionary: theItem];

    }

    return h;

请不要让我使用ASIFormData而不是使用ASIHTTPRequest。谢谢。

1 个答案:

答案 0 :(得分:0)

如果是真正的REST Full实现,您可以使用header参数:Accept:application / json