How to consume post type restful wcf service in iphone?
开发者_JAVA百科I am consuming restful WCF service in the form of JSON of type 'GET'. I want to know how to consume service of type 'POST' so, that i can send large amount of data.
Here is my code for Type 'GET':
NSURL *jsonURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.xxx.com /coffeeiphone/Service.svc/maintransactioninsert/%@/%@/%@",stockid,[format stringFromDate:selected],[quantity text], nil]];
NSString *jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL];
For GET, it works to just get the contents of the URL like you did above. For POST, you will have to create an NSMutableURLRequest.
NSURL *theUrl = [NSURL URLWithString:@"yourURL"];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] initWithURL:theUrl];
[theRequest setHTTPMethod:@"POST"];
//set the body of your request:
[theRequest setHTTPBody: //request here];
//get your response:
NSData *response = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:nil error:nil];
To construct JSON, try looking at the json-framework. https://github.com/stig/json-framework/
精彩评论