Content Length always 0
Content Length always 0
- Subject: Content Length always 0
- From: Brad Soe <email@hidden>
- Date: Tue, 22 Nov 2005 20:10:19 -0800 (PST)
Hi I am trying to use CFHTTPMessage to do a 'POST', but I cannot figure out what is going wrong. I
used the code below to generate headers and the 'body', but I noticed that no matter what the
'Content-Length' header is always set to 0, even though I try to set it to the value using
CFHTTPMessageSetHeaderFieldValue. It could be that I am doing something wrong, but the Apple docs
are not that great. Am I supposed to use CFHTTPMessageSetBody? That does not seem to make a
difference. When should I use it? The current code just creates the message, adds the headers and
uses CFHTTPMessageAppendBytes to add the body of the post (the stuff that the Content-Length is
for). When I do a tcpdump. It looks like everything got into the POST, but the Content-Lenght is
always 0. Can anyone give me any advice? I just want to do a simple post with data attached.
Thanks
Brad
email@hidden
mMessageRef = CFHTTPMessageCreateRequest( kCFAllocatorDefault, CFSTR("POST"), mUrlRef,
kCFHTTPVersion1_0 );
if ( mMessageRef == NULL )
{
CAssert(true, "AFReporter::SendURLReport mMessageRef not initialized");
goto sendFail;
}
mReadStream = CFReadStreamCreateForHTTPRequest( kCFAllocatorDefault, mMessageRef );
if ( mReadStream == NULL )
{
CAssert(true, "AFReporter::SendURLReport mReadStream not initialized");
goto sendFail;
}
if ( CFReadStreamSetProperty(mReadStream, kCFStreamPropertyHTTPShouldAutoredirect,
kCFBooleanTrue) == false )
{
CAssert(true, "AFReporter kCFStreamPropertyHTTPShouldAutoredirect fail");
goto sendFail;
}
theData = CFStringCreateExternalRepresentation(kCFAllocatorDefault, mReportData,
kCFStringEncodingUTF8, 0);
if ( theData == NULL )
{
CAssert(true, "AFReporter::SendURLReport CFStringCreateExternalRepresentation failed");
goto sendFail;
}
DebugCFShow(theData);
dataLen = CFDataGetLength(theData);
cfContentLen = CFStringCreateWithFormat (kCFAllocatorDefault, NULL, CFSTR("%d"), dataLen);
if ( cfContentLen == NULL )
{
CAssert(true, "AFReporter::SendURLReport cfContentLen = NULL");
goto sendFail;
}
DebugCFShow(cfContentLen);
CFHTTPMessageSetBody(mMessageRef, theData);
CFHTTPMessageSetHeaderFieldValue(mMessageRef, CFSTR("User-Agent"), CFSTR("PTUNES9087"));
CFHTTPMessageSetHeaderFieldValue(mMessageRef, CFSTR("Accept"), CFSTR("*.*"));
CFHTTPMessageSetHeaderFieldValue(mMessageRef, CFSTR("Content-Type"),
CFSTR("application/x-www-form-urlencoded"));
CFHTTPMessageSetHeaderFieldValue(mMessageRef, CFSTR("Connection"), CFSTR("close"));
CFHTTPMessageSetHeaderFieldValue(mMessageRef, CFSTR("Host"), CFSTR("log.podbridge.com"));
CFHTTPMessageSetHeaderFieldValue(mMessageRef, CFSTR("Content-Length"), cfContentLen);
buf = malloc(sizeof(UInt8) *dataLen );
if ( buf == NULL )
{
CAssert(true, "AFReporter::SendURLReport buf =NULL");
goto sendFail;
}
CFDataGetBytes (theData, CFRangeMake(0, dataLen), buf);
if (CFHTTPMessageAppendBytes (mMessageRef, buf, dataLen) == false)
DebugPrintf("CFHTTPMessageAppendBytes failed!!!");
CFRelease(theData);
DebugCFShow(mMessageRef);
if (CFHTTPMessageIsHeaderComplete(mMessageRef) == false)
DebugPrintf("CFHTTPMessageIsHeaderComplete not complete!!!");
// Set the client notifier
if ( CFReadStreamSetClient( mReadStream, kNetworkEvents, ReadStreamClientCallBack, &ctxt ) ==
false )
{
CAssert(true, "AFReporter CFReadStreamSetClient fail");
goto sendFail;
}
CFReadStreamScheduleWithRunLoop( mReadStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes );
if (CFReadStreamOpen(mReadStream) == true)
DebugPrintf("CFReadStreamOpen success ");
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden