Hi all ,
I am trying to read a response from a HTTP request. The issue is that I am getting different results when run the binary on Snow-Leopard, Lion and Mountain Lion OS X. The below code works fine on Snow Leopard but not on Lion and ML OS.
Below is the code
#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>
#define YOUTUBE_API_SERVER "
gdata.youtube.com"
#define YOUTUBE_API_VIDEO_FEED "/feeds/api/videos/"
int main(int argc, char *argv[])
{
CFStringRef youtubeURLStr;
youtubeURLStr = CFStringCreateWithFormat(NULL, NULL, CFSTR("http://%s%s%s"), YOUTUBE_API_SERVER, YOUTUBE_API_VIDEO_FEED,"zMa9CT29cQE?fs=1");
CFShow(youtubeURLStr);
CFURLRef cfUrl = CFURLCreateWithString(kCFAllocatorDefault, youtubeURLStr, NULL);
CFHTTPMessageRef cfHttpReq = CFHTTPMessageCreateRequest(kCFAllocatorDefault, CFSTR("GET"), cfUrl, kCFHTTPVersion1_1);
CFReadStreamRef readStream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, cfHttpReq);
CFReadStreamOpen(readStream);
CFHTTPMessageRef cfHttpResp = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, TRUE);
CFIndex numBytesRead;
do
{
const int nBuffSize = 1024;
UInt8 buff[nBuffSize];
numBytesRead = CFReadStreamRead(readStream, buff, nBuffSize);
if( numBytesRead > 0 )
{
CFHTTPMessageAppendBytes(cfHttpResp, buff, numBytesRead);
}
else
if( numBytesRead < 0 )
{
CFStreamError error = CFReadStreamGetError(readStream);
}
} while( numBytesRead > 0 );
CFDataRef cfResp = CFHTTPMessageCopyBody(cfHttpResp);
CFHTTPMessageRef myResponse = (CFHTTPMessageRef)CFReadStreamCopyProperty(readStream, kCFStreamPropertyHTTPResponseHeader);
CFShow(myResponse);
CFShow(cfResp);
CFReadStreamClose(readStream);
CFRelease(cfUrl);
CFRelease(cfHttpReq);
CFRelease(readStream);
CFRelease(cfHttpResp);
return 0;
}
<CFHTTPMessage 0x100109f60>{url = "" href="http://gdata.youtube.com/feeds/api/videos/zMa9CT29cQE?fs=1">http://gdata.youtube.com/feeds/api/videos/zMa9CT29cQE?fs=1; status = HTTP/1.1 200 OK}
<CFData 0x10083b200 [0x7fff7008cee0]>{length = 3711, capacity = 3711, bytes = 0x3c3f786d6c2076657273696f6e3d2731 ... 3c2f656e7472793e}