POST请求的两种方式

  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 }
View Code

 

posted @ 2015-08-31 20:32  海东青飞吧!  阅读(282)  评论(0编辑  收藏  举报