使用initWithContentsOfUrl时如何设置内容类型 [英] How to set content-type when using initWithContentsOfUrl
问题描述
基本上我想设置
Content-Type:application / json;
Content-Type: application/json;
以便打电话一个点网络网络服务,让它返回到一个iPhone应用程序。
in order to call a dot net web service and have it return json to an iphone application.
目前我有
NSString * jsonres = [[NSString alloc] initWithContentsOfURL:url];
为了发出阻止请求,我还需要什么?
What do I need instead in order to issue a blocking request ?
推荐答案
你需要使用NSURLMutableRequest,在这里你可以为内容类型和排序添加标题,这里是参考, http://developer.apple.com/iphone/library /documentation/Cocoa/Reference/Foundation/Classes/NSMutableURLRequest_Class/Reference/Reference.html#//apple_ref/occ/cl/NSMutableURLRequest ,一旦设置了请求,就可以使用此方法添加值 - (void )addValue:(NSString *)值为HTTPHeaderField:(NSString *)字段类似
You need to use NSURLMutableRequest, here you can add headers for content-type and the sort, here is a r eference, http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSMutableURLRequest_Class/Reference/Reference.html#//apple_ref/occ/cl/NSMutableURLRequest, once you set up your request you can add the value using this method - (void)addValue:(NSString *)value forHTTPHeaderField:(NSString *)field something like
[request setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
这篇关于使用initWithContentsOfUrl时如何设置内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!