开发者

iPhone : How to fetch data from web service where web service uses "POST" method for JSON?

I want to fetch data from web service using JSON in my app.

Web service is developed in C# and it uses POST method to pass data.

I found one example but its useful only for GET method?

So How can I fetch JSON data where web service uses POST method?

And I also want to send data to web service. How can I开发者_如何学运维 do that ?


I'm sending POST request in following way (sending xml with some parameters).

    NSString *message = [[NSString alloc] initWithFormat:@"<?xml version=\"1.0\" ?>\n<parameters></parameters>"];

    NSURL *url = [NSURL URLWithString:@"https://www.site.com"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d",[message length]];

    [request addValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request addValue:msgLength                         forHTTPHeaderField:@"Content-Length"];

    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[message dataUsingEncoding:NSUTF8StringEncoding]];
    [message release];

    self.connection = [NSURLConnection connectionWithRequest:request delegate:self];

To collect data you should implement method:

    - (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data

where you should save received data.

In method:

    - (void)connectionDidFinishLoading:(NSURLConnection *)conn

you can parse that data with some JSON-parser.

Hope, that will help you. If you'll have questions about this code ask them in comments.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜