ios cfnetwork发送http请求简单实例
static CFRunLoopRef backgroundRunLoop;
static pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
static pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
void releaseContext(void* infoPtr)
{
CFRelease(infoPtr);
}
void* retainContext(void* infoPtr)
{
CFRetain(infoPtr);
return infoPtr;
}
void cancelTheStream(CFReadStreamRef readStream)
{
CFReadStreamUnscheduleFromRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
CFReadStreamClose(readStream);
CFRelease(readStream);
}
void* threadMain(void* info)
{
pthread_mutex_lock(&mutex);
backgroundRunLoop=CFRunLoopGetCurrent();
CFRunLoopSourceContext context = {0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
CFRunLoopSourceRef source = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context);
CFRunLoopAddSource(CFRunLoopGetCurrent(), source, kCFRunLoopDefaultMode);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
CFRunLoopRun();
}
void createThread(void)
{
pthread_t threadID;
pthread_create(&threadID, NULL,threadMain, NULL);
}
void getResponse(CFReadStreamRef readStream,CFStreamEventType event,void* infoPtr)
{
if(event==kCFStreamEventEndEncountered){
// CFHTTPMessageRef response=CFHTTPMessageCreateEmpty(kCFAllocatorDefault, NO);
CFHTTPMessageRef response=(CFHTTPMessageRef)infoPtr;
UInt32 statusCode=CFHTTPMessageGetResponseStatusCode(response);
CFDataRef responseData=CFHTTPMessageCopyBody(response);
UInt32 count=CFDataGetLength(responseData);
CFStringRef responseJson=CFStringCreateWithBytes(kCFAllocatorDefault, CFDataGetBytePtr(responseData), count, kCFStringEncodingUTF8, YES);
CFRelease(responseJson);
CFRelease(responseData);
cancelTheStream(readStream);
return;
}
if(event==kCFStreamEventHasBytesAvailable){
UInt8 buffer[100];
int length=CFReadStreamRead(readStream, buffer,100);
CFHTTPMessageAppendBytes((CFHTTPMessageRef)infoPtr, buffer, length);
return;
}
if(event==kCFStreamEventErrorOccurred){
//CFStreamError error=CFReadStreamGetError(readStream);
cancelTheStream(readStream);
return;
}
return;
}
@implementation AppDelegate
@synthesize window = _window;
@synthesize versionKey;
@synthesize messageQueueKey;
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//create the url
CFStringRef urlString=CFSTR("your url");
CFURLRef url=CFURLCreateWithString(kCFAllocatorDefault, urlString, NULL);
//create the request method
CFStringRef method=CFSTR("POST");
//create the http request
CFHTTPMessageRef request=CFHTTPMessageCreateRequest(kCFAllocatorDefault, method, url, kCFHTTPVersion1_1);
//set headers
CFStringRef headerName=CFSTR("Content-Type");
CFStringRef headerValue=CFSTR("application/json");
CFHTTPMessageSetHeaderFieldValue(request,headerName,headerValue);
//set body
CFStringRef jsonBody=CFSTR("your json data");
CFDataRef jsonData=CFStringCreateExternalRepresentation(kCFAllocatorDefault, jsonBody, kCFStringEncodingUTF8, 0);
CFHTTPMessageSetBody(request, jsonData);
CFHTTPMessageRef response=CFHTTPMessageCreateEmpty(kCFAllocatorDefault, NO);
CFStreamClientContext context={0,response,retainContext,releaseContext,NULL};
CFOptionFlags registeredEvent=kCFStreamEventHasBytesAvailable|kCFStreamEventEndEncountered|kCFStreamEventErrorOccurred;
CFReadStreamRef readStream=CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, request);
CFReadStreamSetClient(readStream, registeredEvent, getResponse, &context);
if(backgroundRunLoop==NULL){
createThread();
pthread_cond_wait(&cond, &mutex);
}
CFReadStreamScheduleWithRunLoop(readStream, backgroundRunLoop, kCFRunLoopCommonModes);
CFReadStreamOpen(readStream);
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
CFRelease(url);
CFRelease(urlString);
CFRelease(method);
CFRelease(request);
CFRelease(headerName);
CFRelease(headerValue);
CFRelease(jsonBody);
CFRelease(jsonData);
return YES;
}
连接过去和未来,我们为此而存在
摘自 eeeyes
补充:移动开发 , 其他 ,