1 在ViewController中的代碼以下 2 3 - (void)viewDidLoad { 4 [super viewDidLoad]; 5 6 NSString *urlStr1 = @"http://localhost/cgi-bin/post1.cgi"; 7 NSString *urlStr2 = @"http://localhost/cgi-bin/post2.cgi"; 8 9 // [self sendPost1:urlStr1]; 10 [self sendPost2:urlStr2]; 11 } 12 13 #pragma mark -send POST 2- 14 /** 15 使用NSData(二進制數據)承載請求信息,多用於上傳文件 16 */ 17 -(void)sendPost2:(NSString*)urlStr{ 18 //準備工做(規定好了的格式) 19 NSString *head = @"Content-type: multipart/form-data, boundary=AaB03x\n\n"; 20 //規定分隔符 21 NSString *boundary = @"AaB03x"; 22 //頭的分隔符 23 NSString *headBoundary = [NSString stringWithFormat:@"--%@\n",boundary]; 24 //尾的分隔符 25 NSString *endBoundary = [NSString stringWithFormat:@"\n--%@--\n",boundary]; 26 27 //封裝請求頭 28 NSString *contentString = @"content-disposition: form-data; name=\"pic\"; filename=\"star_pressed.png\"Content-Type: image/png\n\n"; 29 30 //建立請求 31 NSURL *url = [NSURL URLWithString:urlStr]; 32 NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url]; 33 34 //對請求操做 35 [request setHTTPMethod:@"POST"]; 36 //設置上傳數據的格式 37 [request addValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"]; 38 39 //設置body 40 NSData *fileData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"star_pressed" ofType:@"png"]]; 41 42 //設置總的請求體 43 NSMutableData *data = [NSMutableData data]; 44 45 //報頭文 = 頭 + 頭分隔符 + 內容說明 + body實體 + 尾分隔符 46 47 [data appendData:[head dataUsingEncoding:NSUTF8StringEncoding]]; 48 [data appendData:[headBoundary dataUsingEncoding:NSUTF8StringEncoding]]; 49 [data appendData:[contentString dataUsingEncoding:NSUTF8StringEncoding]]; 50 [data appendData:fileData]; 51 [data appendData:[endBoundary dataUsingEncoding:NSUTF8StringEncoding]]; 52 53 //設置請求數據的長度 54 [request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)data.length] forHTTPHeaderField:@"Content-Length"]; 55 56 [request setHTTPBody:data]; 57 58 //建立請求,等待返回 59 _connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 60 } 61 62 #pragma mark -send POST 1- 63 /** 64 GET類似,使用url承載請求信息,多用於登錄註冊,填表等操做(這種方式的POST請求也能用GET請求實現,可是GET請求,發送的數據是裸露的) 65 */ 66 -(void)sendPost1:(NSString*)urlStr{ 67 NSURL *url = [NSURL URLWithString:urlStr]; 68 69 NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url]; 70 71 //規定請求方式爲POST請求 72 [request setHTTPMethod:@"POST"]; 73 //要上傳的數據 74 NSString *postStr = @"username=jingjing&password=woxiangjingjing&message=yueme"; 75 //轉化成二進制數據 76 NSData *postData = [postStr dataUsingEncoding:NSUTF8StringEncoding]; 77 //設置請求體 78 [request setHTTPBody:postData]; 79 80 //設置請求數據的長度 81 [request setValue:[NSString stringWithFormat:@"%lu",(unsigned long)postData.length] forHTTPHeaderField:@"Content-Length"]; 82 83 //設置上傳數據的格式 84 [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 85 86 //建立請求,等待返回 87 _connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 88 } 89 90 91 92 #pragma mark -代理方法- 93 94 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 95 96 NSLog(@"接收到響應頭"); 97 NSLog(@"response:%@",response); 98 99 if (_data == nil) { 100 _data = [[NSMutableData alloc]init]; 101 } 102 _data.length = 0; 103 } 104 105 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ 106 [_data appendData:data]; 107 108 NSLog(@"收到數據。。。。:%ld",data.length); 109 110 } 111 112 -(void)connectionDidFinishLoading:(NSURLConnection *)connection{ 113 NSString *str = [[NSString alloc]initWithData:_data encoding:NSUTF8StringEncoding]; 114 NSLog(@"接收數據完畢:\n%@",str); 115 } 116 117 -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ 118 NSLog(@"報錯了:%@",error); 119 } 120 121 122 123 - (void)didReceiveMemoryWarning { 124 [super didReceiveMemoryWarning]; 125 // Dispose of any resources that can be recreated. 126 }