开发者

Http Post with NSData initWithContentsOfURL

I want to do a HTTP post, updating some information to the server

The URL might be "http://www.aaa.com/aaa.aspx?name=John&am开发者_运维百科p;pw=123&age=25"

And I think this method should work:

NSURL* url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData* returnData = [NSData initWithContentsOfURL: url];
[NSString* strRet = [[NSString alloc] initWithData:data encoding:NSUTF8String];

So how could know the update is sucessful? What would be in the NSData returned?

@"200" or some other string returned by the server, such as @"YES"?

Thanks


I think your request would end up being a GET request. And the data returned would be whatever data the web site returns for that URL, not the http headers. I recommend using the excellent ASIHTTPRequest library: http://allseeing-i.com/ASIHTTPRequest/ for communicating with web sites, determining POST/GET mode, etc.


You could use

NSError *error = nil;
NSString *str = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];

and have a look into error to check if the request was successfully.

For bigger requests, please check this link: Objective-C: server requests in a thread (like AsyncTask in Android)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜