//
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