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

100,247

Solution 1

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

Solution 2

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);

Solution 3

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.

Solution 4

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

Solution 5

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/

Share:
100,247

Related videos on Youtube

tarnfeld
Author by

tarnfeld

Updated on August 16, 2020

Comments

  • tarnfeld
    tarnfeld almost 4 years

    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!

  • vikingosegundo
    vikingosegundo about 11 years
    note that ASIHTTPRequest is discontinued by its original author for quite a while now (September 2011). allseeing-i.com/%5Brequest_release%5D;