使用initWithContentsOfUrl时如何设置内容类型

And*_*iih 8 iphone content-type objective-c

基本上我想设置

Content-Type:application/json;

为了调用点网络Web服务并将其返回到iphone应用程序.

目前我有

NSString * jsonres = [[NSString alloc] initWithContentsOfURL:url];

为了发出阻止请求,我还需要什么?

Dan*_*iel 12

您需要使用NSURLMutableRequest,在这里您可以添加内容类型和排序的标题,这里是参考,http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSMutableURLRequest_Class/参考/ Reference.html#// apple_ref/occ/cl/NSMutableURLRequest,一旦设置了你的请求,就可以使用这个方法添加值 - (void)addValue:(NSString*)值为HTTPHeaderField:(NSString*)字段之类的东西

[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
Run Code Online (Sandbox Code Playgroud)