Ios cfnetwork sends simple http request instances

Source: Internet
Author: User

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 };
CFRunLoopSourceRef source = CFRunLoopSourceCreate (kCFAllocatorDefault, 0, & context );
CFRunLoopAddSource (CFRunLoopGetCurrent (), source, kcfrunloopdefamode );
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 didfinishlaunchingwitexceptions :( 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;
}

Connecting the past and the future.

 

From eeeyes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.