Objective C – HTTP POSTor GET Data
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”
Thanks for this post its going to save me a lot of headache!
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
@Martin Eigo
Thanks for sharing