开发者

Tutorials for using HTTP POST and GET on the iPhone in Objective-C [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

Questions asking us to 开发者_如何学JAVArecommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.

Closed 9 years ago.

Improve this question

I downloaded apple's demo for using HTTP POST and GET (Their sample app has a tabbar with different parts) and the code is so confusing!

Could anybody give me some sample code or a link to some tutorials about it? :)

Thanks!


This walkthrough by Matt Long is particularly good: http://www.cimgf.com/2010/02/12/accessing-the-cloud-from-cocoa-touch/

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] 
        initWithURL:[NSURL 
        URLWithString:@"http://www.cimgf.com/testpost.php"]];
 
[request setHTTPMethod:@"POST"];
[request setValue:@"text/xml" 
              forHTTPHeaderField:@"Content-type"];
 
NSString *xmlString = @"<data><item>Item 1</item><item>Item 2</item></data>";
 
[request setValue:[NSString stringWithFormat:@"%d",
        [xmlString length]] 
        forHTTPHeaderField:@"Content-length"];
 
[request setHTTPBody:[xmlString 
        dataUsingEncoding:NSUTF8StringEncoding]];
 
[[NSURLConnection alloc] 
        initWithRequest:request 
                   delegate:self];


this is a simple way to use GET:

   NSURL *url = [NSURL URLWithString:@"http://www.32133.com/test?name=xx"];
   NSData *data = [NSData dataWithContentsOfURL:url];
   NSString *ret = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
   NSLog(@"ret=%@", ret);


you may also want to check this sample

ASIFormDataRequest  *request =
     [[[ASIFormDataRequest  alloc]  initWithURL:url] autorelease];
[request setPostValue:@"Ben" forKey:@"first_name"];
[request setPostValue:@"Copsey" forKey:@"last_name"];
[request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];

You can find more details here: http://www.redcodelabs.com/2009/08/objective-c-http-post-get-data/


There are many ways to implement HTTP Request in objective-c , CFNetwork library is designed for this purpose but the easiest way to develop http request is to use NSURLConnection.

Here is a sample :

NSURLConnection *cmdConn = [[[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES] autorelease];

you will receive the response in didreceiveResponse: and didreceiveData: delegates.


hope this will help you, if you want to post a form:

NSMutableURLRequest *request = 
        [[NSMutableURLRequest alloc] initWithURL:
            [NSURL URLWithString:@"http://www.32133.com/test"]];

[request setHTTPMethod:@"POST"];

NSString *postString = @"name=ivybridge&description=This%20is%20desp.";

[request setValue:[NSString 
        stringWithFormat:@"%d", [postString length]] 
        forHTTPHeaderField:@"Content-length"];

[request setHTTPBody:[postString 
        dataUsingEncoding:NSUTF8StringEncoding]];

[[NSURLConnection alloc] 
        initWithRequest:request delegate:self];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜