Making an HTTP Connection in iPhone Application

By: Jonathan Zdziarski Viewed: 153170 times    

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

    if (!readStream) {
        return -1;

    if (!CFReadStreamSetClient(readStream, kCFStreamEventOpenCompleted |
                                           kCFStreamEventHasBytesAvailable |
                                           kCFStreamEventEndEncountered |
        ReadCallBack, &CTX))
        return -1;    }

        /* Add to the run loop */
    CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(),

    if (!CFReadStreamOpen(readStream)) {
        CFReadStreamSetClient(readStream, 0, NULL, NULL);
        return -1;

    return 0;

Most Viewed Articles (in iPhone )

Latest Articles (in iPhone)

Comment on this tutorial