CFNetwork 网络请求2

//

//  CFNetworkTest2.m

//  TestCF

//

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

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

//


#import "CFNetworkTest2.h"

#import <CoreFoundation/CoreFoundation.h>

#import <CFNetwork/CFNetwork.h>


@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


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。