Making an HTTP Connection in iPhone Application

By: Jonathan Zdziarski Emailed: 1730 times Printed: 2344 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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;
}

iPhone Home | All iPhone Tutorials | Latest iPhone Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Jonathan Zdziarski
Making an FTP Connection in iPhone Application
Making an HTTP Connection in iPhone Application
Directory Structure for iPhone Applications

More Tutorials in iPhone
Making an HTTP Connection in iPhone Application
Making an FTP Connection in iPhone Application
Directory Structure for iPhone Applications
Multitasking in iPhone 4 - iOS 4

More Latest News
Most Viewed Articles (in iPhone )
Multitasking in iPhone 4 - iOS 4
Directory Structure for iPhone Applications
Making an HTTP Connection in iPhone Application
Making an FTP Connection in iPhone Application
Most Emailed Articles (in iPhone)
Multitasking in iPhone 4 - iOS 4
Directory Structure for iPhone Applications
Making an HTTP Connection in iPhone Application
Making an FTP Connection in iPhone Application