//
// CFNetworkTest3.m
// TestCF
//
// Created by Jabez on 15/5/11.
// Copyright (c) 2015年 John. All rights reserved.
//
#import "CFNetworkTest3.h"
@implementation CFNetworkTest3
// good
- (void)sendMessage
{
[self request];
}
- (void)request
{
CFStringRef url = CFSTR("http://jabez.local/~jabez/test/response/response0");
CFURLRef myURL = CFURLCreateWithString(kCFAllocatorDefault, url, NULL);
CFStringRef requestMethod = CFSTR("POST");
CFHTTPMessageRef myRequest = CFHTTPMessageCreateRequest(kCFAllocatorDefault, requestMethod, myURL, kCFHTTPVersion1_1);
NSData *bodyData = [@"key=test3" dataUsingEncoding:NSUTF8StringEncoding];
CFHTTPMessageSetBody(myRequest, (__bridge CFDataRef)bodyData);
CFHTTPMessageSetHeaderFieldValue(myRequest, CFSTR("key"), CFSTR("testheader3"));
CFReadStreamRef requestReadStream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, myRequest);
CFHTTPMessageRef response = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, false);
CFStreamClientContext clientConetext = { 0, response, NULL, NULL, NULL};
CFOptionFlags flags = kCFStreamEventHasBytesAvailable | kCFStreamEventEndEncountered | kCFStreamEventErrorOccurred;
Boolean result = CFReadStreamSetClient(requestReadStream, flags, myCFReadStreamClientCallback, &clientConetext);
if (result) {
CFReadStreamScheduleWithRunLoop(requestReadStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
if (CFReadStreamOpen(requestReadStream)) {
CFRunLoopRun();
} else {
CFReadStreamUnscheduleFromRunLoop(requestReadStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
}
}
CFRelease(myURL);
CFRelease(myRequest);
}
static void myCFReadStreamClientCallback(CFReadStreamRef stream, CFStreamEventType type, void *clientCallBackInfo) {
CFHTTPMessageRef response = (CFHTTPMessageRef)clientCallBackInfo;
if (type == kCFStreamEventEndEncountered) {
CFIndex statusCode = CFHTTPMessageGetResponseStatusCode(response);
NSLog(@"status Code: %li", statusCode);
CFDataRef responseData = CFHTTPMessageCopyBody(response);
CFIndex count = CFDataGetLength(responseData);
CFStringRef responseJson = CFStringCreateWithBytes(kCFAllocatorDefault, CFDataGetBytePtr(responseData), count, kCFStringEncodingUTF8, YES);
NSString *jsongString = [NSString stringWithFormat:@"%@", (__bridge NSString *) responseJson];
NSLog(@"jsongString: %@", jsongString);
CFRelease(responseJson);
CFRelease(responseData);
} else if (type == kCFStreamEventErrorOccurred) {
CFReadStreamUnscheduleFromRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
CFReadStreamClose(stream);
CFRelease(stream);
stream = NULL;
} else if (type == kCFStreamEventHasBytesAvailable) {
CFTypeRef message = CFReadStreamCopyProperty(stream, kCFStreamPropertyHTTPResponseHeader);
NSDictionary *httpHeaders =
(__bridge NSDictionary *)CFHTTPMessageCopyAllHeaderFields((CFHTTPMessageRef) message);
NSLog(@"dict: %@", httpHeaders);
CFRelease(message);
UInt8 buffer[1024];
CFIndex length = CFReadStreamRead(stream, buffer, sizeof(buffer));
CFHTTPMessageAppendBytes(response, buffer, length);
}
}
@end