如何在iphone中使用post类型的restful wcf服务?

时间:2011-05-21 05:26:39

标签: iphone objective-c wcf rest post

我正在以“GET”类型的JSON形式使用restful WCF服务。 我想知道如何使用'POST'类型的服务,这样我就可以发送大量数据。

这是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];   

1 个答案:

答案 0 :(得分:1)

对于GET,它可以像上面一样获取URL的内容。 对于POST,您必须创建一个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];

要构建JSON,请尝试查看json-framework。 https://github.com/stig/json-framework/

相关问题