ios 네트워크 요청 코드

12008 단어 ios
실제 서버 IP 1 을 숨 기 고 CFHTTPMessage 등 을 사용 합 니 다.
 
 

 

    CFURLRef myWebserverURLRef = CFURLCreateWithString(kCFAllocatorDefault, CFSTR("http://192.168.0.1234"), NULL);

    if(myWebserverURLRef != NULL)

    {

        // Create the HTTP message

        CFHTTPMessageRef theMessageRef = CFHTTPMessageCreateRequest(kCFAllocatorDefault, CFSTR("POST"), myWebserverURLRef, kCFHTTPVersion1_1);

        if(theMessageRef != NULL)

        {

            // Set useful headers

            CFHTTPMessageSetHeaderFieldValue(theMessageRef, CFSTR("Accept"), CFSTR("application/xml;text/vnd.wap.wml;text/*,image/jpeg"));

            CFHTTPMessageSetHeaderFieldValue(theMessageRef, CFSTR("Content-type"), CFSTR("application/html"));

            CFHTTPMessageSetHeaderFieldValue(theMessageRef, CFSTR("Host"),CFSTR("192.168.0.1:1234"));

            CFHTTPMessageSetHeaderFieldValue(theMessageRef, CFSTR("Connection"),CFSTR("Close"));

            CFHTTPMessageSetHeaderFieldValue(theMessageRef, CFSTR("Accept-Charset"),CFSTR("utf-8;q=0.7,*;q=0.7"));

            CFHTTPMessageSetHeaderFieldValue(theMessageRef, CFSTR("Content-Length"),CFSTR("688"));

 

 

            // The body

            NSString* filepath = [[NSString alloc] initWithFormat:@"%@/body.txt",[[NSBundle mainBundle] bundlePath]];

            NSData *body = [NSData dataWithContentsOfFile:filepath];

            CFHTTPMessageSetBody(theMessageRef, (CFDataRef)body);

 

 

            // Create the CFReadStreamRef

            streamRef = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, theMessageRef);

            if(streamRef != NULL)

            {            

                if(true)

                {

 

                    int myHTTPSProxyPortNumber = 80;

                    CFTypeRef keys[2], values[2];

                    keys[0] = kCFStreamPropertyHTTPProxyHost;

                    values[0] = CFSTR("10.0.0.172");

                    keys[1] = kCFStreamPropertyHTTPProxyPort;

                    values[1] = CFNumberCreate(NULL, kCFNumberSInt32Type, &myHTTPSProxyPortNumber);

                    CFDictionaryRef proxyDict = CFDictionaryCreate(NULL, keys, values, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

                    CFReadStreamSetProperty(streamRef, kCFStreamPropertyHTTPProxy, proxyDict);

                }

                CFStreamClientContext streamcontext = {0, self, NULL, NULL, NULL};

                CFOptionFlags eventflags = kCFStreamEventOpenCompleted | kCFStreamEventHasBytesAvailable 

                | kCFStreamEventEndEncountered | kCFStreamEventErrorOccurred;

 

                if (!CFReadStreamSetClient(streamRef, eventflags, ReadStreamClientCallBack, &streamcontext))

                {

 

                    return ;

                }

 

                CFReadStreamScheduleWithRunLoop(streamRef, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);

 

2.NSURL 사용
 
 

    NSURL *url = [NSURL  URLWithString:@"http://192.168.0.1:1234/"];

    NSMutableURLRequest *request = [NSMutableURLRequest  requestWithURL:url];

    [request setURL: url ];

    NSString* filepath = [[NSString alloc] initWithFormat:@"%@/body.txt",[[NSBundle mainBundle] bundlePath]];

    NSData *body = [NSData dataWithContentsOfFile:filepath];

    [filepath release];

 

    [request setHTTPBody: body];

    [request setHTTPMethod: @"POST"];

 

//    xuepomh  

    [request setValue:@"Close" forHTTPHeaderField:@"Connection"];

    [request setValue:@"192.168.0.1:1234" forHTTPHeaderField:@"Host"];

    [request setValue:@"688" forHTTPHeaderField:@"Content-Length"];

   NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:true];

 

 

3\socket
 
 

 

connect()

send()

while(1)

{

recv()

}

 

좋은 웹페이지 즐겨찾기