// // CFNetworkTest1.m // TestCF // // Created by Jabez on 15/5/11. // Copyright (c) 2015年 John. All rights reserved. // #import "CFNetworkTest1.h" @implementation CFNetworkTest1 - (void)sendMessage { [self magicHappens]; } - (void)magicHappens { CFHTTPMessageRef request = [self buildMeaage]; CFHTTPMessageRef response = [self performHTTPRequest:request]; CFIndex statusCode; statusCode = CFHTTPMessageGetResponseStatusCode(response); // An HTTP status code of 401 or 407 indicates that authentication is // required. I use an auth count to make sure we don't get stuck in an // infinite loop if our credentials are bad. Sometimes, making the // request more tha once lets it go through. // I admit I don't know why. // int authCount = 0; // while ((statusCode == 401 || statusCode == 407) && authCount < 3) { // request = [self buildMeaage]; // [self addAuthentionToRequest:request withResponse:response]; // // response = [self performHTTPRequest:request]; // statusCode = CFHTTPMessageGetResponseStatusCode(response); // authCount++; // } CFDataRef responseDataRef = CFHTTPMessageCopyBody(response); NSData *responseData = (__bridge NSData *)responseDataRef; NSString *responseBody = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; NSLog(@"responseBody: %@", responseBody); } - (CFHTTPMessageRef)buildMeaage { NSURL *url = [NSURL URLWithString:@"http://jabez.local/~jabez/test/response/response0"]; NSData *dataToPost = [@"key=test1" dataUsingEncoding:NSUTF8StringEncoding]; // create with the default allocator (NULL), a post request, // the uRL, and pick either // kCFHTTPVersion1_0 or kCFHTTPVersion1_1 CFHTTPMessageRef request = CFHTTPMessageCreateRequest(NULL, CFSTR("POST"), (__bridge CFURLRef)url, kCFHTTPVersion1_1); CFHTTPMessageSetBody(request, (__bridge CFDataRef) dataToPost); // Unfortunately, this isn't smart enough to set reasonalle headers for you CFHTTPMessageSetHeaderFieldValue(request, CFSTR("HOST"), (__bridge CFStringRef)[url host]); CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Content-Length"), (__bridge CFStringRef) [NSString stringWithFormat:@"%li", [dataToPost length]]); CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Content-Type"), CFSTR("charset=utf-8")); return ((CFHTTPMessageRef) CFAutorelease(request)); } - (CFHTTPMessageRef)performHTTPRequest:(CFHTTPMessageRef)request { CFReadStreamRef requestStream = CFReadStreamCreateForHTTPRequest(NULL, request); CFReadStreamOpen(requestStream); NSMutableData *responseBytes = [NSMutableData data]; CFIndex numBytesRead = 0; do { UInt8 buf[1024]; numBytesRead = CFReadStreamRead(requestStream, buf, sizeof(buf)); if (numBytesRead > 0) { [responseBytes appendBytes:buf length:numBytesRead]; } } while (numBytesRead > 0); CFHTTPMessageRef response = (CFHTTPMessageRef) CFReadStreamCopyProperty(requestStream, kCFStreamPropertyHTTPResponseHeader); CFHTTPMessageSetBody(response, (__bridge CFDataRef)responseBytes); CFReadStreamClose(requestStream); CFRelease(requestStream); return ((CFHTTPMessageRef) CFAutorelease(response)); } - (Boolean)addAuthentionToRequest:(CFHTTPMessageRef)request withResponse:(CFHTTPMessageRef)response { CFHTTPAuthenticationRef authentication = CFHTTPAuthenticationCreateFromResponse(NULL, response); CFStreamError err; Boolean success = CFHTTPMessageApplyCredentials(request, authentication, CFSTR("username"), CFSTR("password"), &err); return success; } @end
时间: 2024-10-23 17:20:50