views:

261

answers:

1

Basically I want to set

Content-Type: application/json;

in order to call a dot net web service and have it return json to an iphone application.

At the moment I have

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

What do I need instead in order to issue a blocking request ?

+1  A: 

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%5FClass/Reference/Reference.html#//apple%5Fref/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 addValue:@"json" forHttpHeaderField:@"Content-type"]
Daniel
Yep, I think that will do it. Thanks for the quick response!
Andiih
although its not a blocking style request, instead using delgates and callbacks - which will involve some re-architecting!
Andiih
for blokcing style you just gotta do an sync call (forgot to mention this sorry) you can use NSURLConnections method called + (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error, heres a link http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html#//apple_ref/occ/clm/NSURLConnection/sendSynchronousRequest:returningResponse:error:
Daniel
Thanks. Just figured that out myself and came back here to change my comments :-) You got there 5 hours ago!
Andiih
Please don't use sendSynchronousRequest from the main thread. It results in apps that perform poorly.
rpetrich
This is true sometimes
Daniel