Cfnetwork Network Request 2

Source: Internet
Author: User

//

Cfnetworktest2.m

Testcf

//

Created by Jabez on 15/5/11.

Copyright (c) year 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 = [nsmutabledatadata];

[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 = [Bodydatausingencoding:nsutf8stringencoding];

Cfhttpmessagesetbody (Myrequest, (__bridgecfdataref) 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(), KCFR Unloopcommonmodes);

}

}

Cfrelease (Myurl);

Cfrelease (myrequest);

}


-(void) Requesterror

{

NSLog(@ "request Error");

}


-(void) Finishrequestok

{

nsstring *responsestring = [[nsstringalloc] initwithdata :_responsedataencoding:nsutf8stringencoding];

NSLog (@ "resposnestring:%@", responsestring);

}


-(void) Appendbytes: (constvoid *) bytes Length: (nsuinteger) length

{

[_responsedataappendbytes: bytes length: length];

}


Static void mycfreadstreamclientcallback (cfreadstreamref stream,cfstreameventtype type, void *cientcallbackinfo) {

CFNetworkTest2 *delegate = (__bridgeCFNetworkTest2 *) Cientcallbackinfo;

if (type = =kcfstreameventendencountered) {

[DelegateFinishrequestok];

} elseif (type = =kcfstreameventerroroccurred) {

Cfreadstreamunschedulefromrunloop(stream,cfrunloopgetcurrent(), Kcfrunloopcommonmodes);

Cfreadstreamclose (stream);

Cfrelease (stream);

stream =NULL;

[DelegateRequesterror];

} elseif (type = =kcfstreameventhasbytesavailable) {

UInt8 buffer[];

Cfindex Numbytesread;

Numbytesread =Cfreadstreamread(stream, buffer, sizeof(buffer));

[Delegateappendbytes: Buffer length: numbytesread];

}

}




@end


Cfnetwork Network Request 2

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.