// Created by Alexandre Colucci on 23/07/2008.

// Copyright 2008 Alexandre Colucci. All rights reserved.


int main(int argc, char* argv[])

{

// The URL of the Webserver

CFURLRef myWebserverURLRef = CFURLCreateWithString(kCFAllocatorDefault, CFSTR("https://myWebserver.com/"), NULL);

if(myWebserverURLRef != NULL)

{

// Create the HTTP message

CFHTTPMessageRef theMessageRef = CFHTTPMessageCreateRequest(kCFAllocatorDefault, CFSTR("POST"), myWebserverURLRef, kCFHTTPVersion1_1);

if(theMessageRef != NULL)

{

// Set useful headers

CFHTTPMessageSetHeaderFieldValue(theMessageRef, CFSTR("Accept"), CFSTR("text/xml"));

CFHTTPMessageSetHeaderFieldValue(theMessageRef, CFSTR("Content-type"), CFSTR("application/xml"));

// The body

char *theDataString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><something></something>";

CFDataRef dataRef = CFDataCreate(kCFAllocatorDefault, (const UInt8*)theDataString, strlen(theDataString));

CFHTTPMessageSetBody(theMessageRef, dataRef);

// Create the CFReadStreamRef

CFReadStreamRef streamRef = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, theMessageRef);

if(streamRef != NULL)

{

// We don't want to validate the HTTPS certificate.

CFMutableDictionaryRef securityDictRef = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

if(securityDictRef != NULL)

{

CFDictionarySetValue(securityDictRef, kCFStreamSSLValidatesCertificateChain, kCFBooleanFalse);

CFReadStreamSetProperty(streamRef, kCFStreamPropertySSLSettings, securityDictRef);

CFRelease(securityDictRef);

}

// Open the stream

CFReadStreamOpen(streamRef);

// Read the data

CFIndex theBufferLength = 1024;

UInt8 theBuffer[1024];

CFIndex theBytesRead = 0;

CFMutableStringRef theMutableStringRef = CFStringCreateMutable( kCFAllocatorDefault, 0 );

do

{

theBytesRead = CFReadStreamRead(streamRef, theBuffer, theBufferLength);

if(theBufferLength > 0)

CFStringAppendCString(theMutableStringRef, (const char*)theBuffer, kCFStringEncodingASCII);

}

while(theBytesRead > 0);

if(theBytesRead < 0)

fprintf(stderr, "Host is down?\n");

else

{

fprintf(stderr, "didReceiveData: \n");

CFShow(theMutableStringRef);

}

CFRelease(theMutableStringRef);

CFRelease(streamRef);

}

CFRelease(dataRef);

CFRelease(theMessageRef);

}

CFRelease(myWebserverURLRef);

}

return 0;

}