Making an HTTP Connection in iPhone Application
By: Jonathan Zdziarski in iPhone Tutorials on 2010-09-06
You can use the CFHTTP API to create an HTTP request. This allows you to easily invoke HTTP GET, HEAD, PUT, POST, and most other standard requests. Creating a request involves the three-step process of creating the request object, defining the HTTP request message and headers, and serializing the message into raw protocol. Only HTTP POST requests generally contain a message body, which can contain POST form data to send. All other requests use an empty body while embedding the request parameters into the headers.In the example below, an HTTP/1.1 GET request is created, specifying the URL http://www.oreilly.com and setting the Connection header to instruct the remote end to close the connection after sending data:
CFStringRef requestHeader = CFSTR("Connection"); CFStringRef requestHeaderValue = CFSTR("close"); CFStringRef requestBody = CFSTR(""); CFStringRef url = CFSTR("http://www.oreilly.com">http://www.oreilly.com"); CFStringRef requestMethod = CFSTR("GET"); CFURLRef requestURL = CFURLCreateWithString(kCFAllocatorDefault, url, NULL); CFHTTPMessageRef request = CFHTTPMessageCreateRequest(kCFAllocatorDefault, requestMethod, requestURL, kCFHTTPVersion1_1); CFHTTPMessageSetBody(request, requestBody); CFHTTPMessageSetHeaderFieldValue(request, requestHeader, requestHeaderValue); CFDataRef serializedRequest = CFHTTPMessageCopySerializedMessage(request);
The resulting pointer to a CFData structure provides the raw HTTP protocol output, which you would then send through a write stream to the destination server. In the example below, an HTTP GET request is created and opened through a read stream. As data flows in, the read stream's callbacks would normally be invoked to receive the new data:
int makeRequest(const char *requestURL) { CFReadStream readStream; CFHTTPMessageRef request; CFStreamClientContext CTX = { 0, NULL, NULL, NULL, NULL }; NSString* requestURLString = [ [ NSString alloc ] initWithCString: requestURL ]; NSURL url = [ NSURL URLWithString: requestURLString ]; CFStringRef requestMessage = CFSTR(""); request = CFHTTPMessageCreateRequest(kCFAllocatorDefault, CFSTR("GET"), (CFURLRef) url, kCFHTTPVersion1_1); if (!request) { return -1; } CFHTTPMessageSetBody(request, (CFDataRef) requestMessage); readStream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, request); CFRelease(request); if (!readStream) { return -1; } if (!CFReadStreamSetClient(readStream, kCFStreamEventOpenCompleted | kCFStreamEventHasBytesAvailable | kCFStreamEventEndEncountered | kCFStreamEventErrorOccurred, ReadCallBack, &CTX)) { CFRelease(readStream); return -1; } /* Add to the run loop */ CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); if (!CFReadStreamOpen(readStream)) { CFReadStreamSetClient(readStream, 0, NULL, NULL); CFReadStreamUnscheduleFromRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); CFRelease(readStream); return -1; } return 0; }
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in iPhone ) |
Latest Articles (in iPhone) |
Comments