Home > iPhone SDK > Objective C – HTTP POSTor GET Data

Objective C – HTTP POSTor GET Data

August 14th, 2009 Leave a comment Go to comments

ASIHTTPRequest is an easy to use wrapper around the CFNetwork API that makes some of the more tedious aspects of communicating with web servers easier. It is written in Objective-C and works in both Mac OS X and iPhone applications.

It is suitable performing basic HTTP requests and interacting with REST-based services (GET / POST / PUT / DELETE). The included ASIFormDataRequest subclass makes it easy to submit POST data and files using multipart/form-data.

Here is an sample code on how easy is to make a http-post from objective c.

(Source: http://allseeing-i.com/ASIHTTPRequest/How-to-use)

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

How to install and use ASIHTTPRequest in an iPhone project ?

1) Add the files

Copy the files you need to your project folder, and add them to your Xcode project. An overview of the ASIHTTPRequest source files appears here.

2) Link against CFNetwork, SystemConfiguration and zlib

Choose Project -> Edit Active Target from the menu.

Click the plus button in the bottom left of the window.

Choose “CFNetwork.framework” from the list, and click Add.

Repeat the same operation for “SystemConfiguration.framework” and “libz.1.2.3.dylib”

Bookmark and Share
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogosphere News
  • LinkedIn
  • Live
  • MySpace
  • Tumblr
  • TwitThis
  • Yahoo! Buzz
  • StumbleUpon
  • Technorati
Categories: iPhone SDK Tags: , , ,
  1. February 12th, 2010 at 07:21 | #1

    Thanks for this post its going to save me a lot of headache!

  2. June 29th, 2010 at 17:17 | #2

    If you want to get the response message as a string then you can use this code

    NSError *oError = [[NSError alloc] init];
    NSHTTPURLResponse *oResponseCode = nil;
    NSData *oResponseData = [NSURLConnection sendSynchronousRequest:oRequest returningResponse:oResponseCode error:oError]
    NSString * strResult = [[NSString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding];

    Check out this article for more info http://www.eigo.co.uk/iPhone-Submitting-HTTP-Requests.aspx

  3. June 29th, 2010 at 20:13 | #3

    @Martin Eigo
    Thanks for sharing ;)

  1. August 18th, 2009 at 08:55 | #1