CFNetwork 网络请求2

//

//  CFNetworkTest2.m

//  TestCF

//

//  Created by Jabez on 15/5/11.

//  Copyright (c) 2015 John. All rights reserved.

//


#import "CFNetworkTest2.h"

#import

#import


@interface CFNetworkTest2 ()


@property (nonatomic, strong) NSMutableData *responseData;


@end


@implementation CFNetworkTest2


- (void)sendMessage

{

    self.responseData = [NSMutableData data];

    

    [self sendRequest];

}


- (void)sendRequest

{

    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);

    

    NSString *body = @"key=cfnetworkTest2";

    NSData *bodyData = [body dataUsingEncoding:NSUTF8StringEncoding];

    CFHTTPMessageSetBody(myRequest, (__bridge CFDataRef)bodyData);

    

    CFHTTPMessageSetHeaderFieldValue(myRequest, CFSTR("key"), CFSTR("headValue2"));

    

    CFReadStreamRef requestReadStream = CFReadStreamCreateForHTTPRequest(NULL, myRequest);

    CFStreamClientContext clientContext =  { 0, (__bridge void *)(self), NULL, NULL, NULL };

    CFOptionFlags flags = kCFStreamEventHasBytesAvailable |

                            kCFStreamEventEndEncountered |

                            kCFStreamEventErrorOccurred;

    Boolean result = CFReadStreamSetClient(requestReadStream, flags, myCFReadStreamClientCallback, &clientContext);

    if (result) {

        CFReadStreamScheduleWithRunLoop(requestReadStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);

        if (CFReadStreamOpen(requestReadStream)) {

            CFRunLoopRun();

        } else {

            CFReadStreamUnscheduleFromRunLoop(requestReadStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);

        }

    }

    

    CFRelease(myURL);

    CFRelease(myRequest);

}


- (void)requestError

{

    NSLog(@"request error");

}


- (void)finishRequestOK

{

    NSString *responseString = [[NSString alloc] initWithData:_responseData encoding:NSUTF8StringEncoding];

    NSLog(@"resposneString: %@", responseString);

}


- (void)appendBytes:(const void *)bytes length:(NSUInteger)length

{

    [_responseData appendBytes:bytes length:length];

}


static void myCFReadStreamClientCallback(CFReadStreamRef stream, CFStreamEventType type, void *cientCallbackInfo) {

    CFNetworkTest2 *delegate = (__bridge CFNetworkTest2 *)cientCallbackInfo;

    

    if (type == kCFStreamEventEndEncountered) {

        [delegate finishRequestOK];

    } else if (type == kCFStreamEventErrorOccurred) {

        CFReadStreamUnscheduleFromRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);

        CFReadStreamClose(stream);

        CFRelease(stream);

        stream = NULL;

        [delegate requestError];

    } else if (type == kCFStreamEventHasBytesAvailable) {

        UInt8 buffer[1024];

        CFIndex numBytesRead;

        numBytesRead = CFReadStreamRead(stream, buffer, sizeof(buffer));

        [delegate appendBytes:buffer length:numBytesRead];

    }

}




@end


你可能感兴趣的:(IOS)