HTTP example for I-Phone
I am searching for HTTP example for I-phone 3.0. i am doing SyncMl application , which is uses http based protocol to syncronise two databases (ie client and server ) Using POST and GET.So i will开发者_如何学JAVA be sending data to server using POST and reading data using GET.If anybody has sample code or any hint what type framework would be used ??
I am going to assume you are interested in implementing some plain old HTTP client code. Requesting a web page etc.
I use NSURL to do my HTTP requests. It is pretty straightforward. You can read all about it on the NSURL Class Reference, but here is a snippet of sample code:
// set up your request
NSURL * url = [NSURL URLWithString:@"http://www.stackoverflow.com"];
NSURLRequest * request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60];
// create your connection with your request and a delegate (in this case
// the object making the request)
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
You just need to implement some delegate methods to handle the data responses
- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSHTTPURLResponse*)response
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
ASIHTTPRequest is a great framework for HTTP Requests.
From the ASIHTTPRequest site:
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.
There is also a Google group. The code is hosted on github.
精彩评论