CFReadStreamOpen function does not serialize and send HTTP request
I wanted to try the example code given in the CFNetwork
programming guide under sub-section 'Communicating with HTTP servers'. I tried the same example code, but the problem is it just opens the stream it does not send the http request. The exact code which I wrote in my application is as below. According to the documentation when I call the CFReadStreamOpen
function it serializes and sends the http request. But when I see the packets in the wireshark. I can see just three TCP packets which are actually packlets of TCP connection three way handshake. So what's the problem????
CFStringRef bodyData = CFSTR("mydata is here"); // Usually used for POST data
CFStringRef headerFieldName = CFSTR("X-My-Favorite-Field");
CFStringRef headerFieldValue = CFSTR("Dreams");
CFStringRef url = CFSTR("http://www.darshan.com");
CFURLRef myURL = CFURLCreateWithString(kCFAllocatorDefault, url, NULL);
CFStringRef requestMethod = CFSTR("POST");
CFHTTPMessageRef myRequest =
CFHTTPMessageCreateRequest(kCFAllocatorDefault, requestMethod, myURL,
kCFHTTPVersion1_1);
CFHTTPMessageSetBody(myRequest, bodyData);
CFHTTPMessageSetHeaderFieldValue(myRequest, headerFieldName, headerFieldValue);
CFReadStreamRef myReadStream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, myRequest);
CFReadStreamOpen(myReadStream开发者_运维问答);
here is the solution , when you create the CFHTTPMessage
you should serialize it (for example in my code I used UTF8 ) and then write to to server .
here is my code sample you can follow this example it is working great :
CFHTTPMessageRef myRequest = CFHTTPMessageCreateRequest(kCFAllocatorDefault,CFSTR("POST"), myURL, kCFHTTPVersion1_1);
CFHTTPMessageSetHeaderFieldValue(myRequest, CFSTR("Content-Type"), CFSTR("application/x-www-form-urlencoded"));
CFHTTPMessageSetHeaderFieldValue(myRequest, CFSTR("Content-Length"), (CFStringRef)postLength);
CFHTTPMessageSetBody(myRequest, (CFDataRef)postData); // postData is NSData
dataReadStream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, myRequest);
//// From here Is Connection To Server
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault,
(CFStringRef) serverHost,
serverPort,
&dataReadStream,
&dataWriteStream);
if (dataReadStream && dataWriteStream) {
CFReadStreamSetProperty(dataReadStream,
kCFStreamPropertyShouldCloseNativeSocket,
kCFBooleanTrue);
CFWriteStreamSetProperty(dataWriteStream,
kCFStreamPropertyShouldCloseNativeSocket,
kCFBooleanTrue);
iStream = (NSInputStream *)dataReadStream;
[iStream retain];
[iStream setDelegate:self];
[iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[iStream open];
oStream = (NSOutputStream *)dataWriteStream;
[oStream retain];
[oStream setDelegate:self];
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
forMode:NSDefaultRunLoopMode];
[oStream open];
///Now Write Your Command To Server
NSString *content = [NSString stringWithFormat:@"Your Command"];
NSString *req = [NSString stringWithFormat:@"POST /client HTTP/1.1\r\nContent-Length: %d\r\n\r\n%@\r\n", [content length], content];
[oStream write:(const UInt8 *)[req UTF8String] maxLength:strlen((char*)[req UTF8String])];
then receive the notification in just dont Forget to add to header file
-(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
精彩评论