iOS put form-data请求

https://www.cnblogs.com/louzhuhaha/p/3633402.html

ios 使用AFNetWorking 但是将form-data加入头之后,服务器看到传入的参数是空,结果网上翻了半天资料,说是ASI可以的,但是无奈,这个库太老了,并且也尝试了并没有成功,下面载自大神的博客了,将post改为put使用底层的 实现的。

iOS网络请求之multipart/form-data提交数据

multipart/form-data表单数据
在http网络请求中,post没有请求长度的限制,因为post把数据放在了body中,而不是像Get一样放在了浏览器的地址栏中(可以这么理解),
所以相对安全
POST有两种方式
第一种直接把数据放在body中,用contentType来区分类型是text还是json或者是别的什么数据。这个最简单,不做赘述。
第二种是表单的形式,通过boundaries来区分放置的是那些数据,很像一个字典,用K,V放置对象。
参考POST表单数据,这是摘自网上的一段Http请求代码

POST /upload_file/UploadFile HTTP/1.1 
Accept: text/plain, */* 
Accept-Language: zh-cn 
Host: 192.168.29.65:80 
Content-Type:multipart/form-data;boundary=---------------------------7d33a816d302b6
User-Agent: Mozilla/4.0 (compatible; OpenOffice.org) 
Content-Length: 424 
Connection: Keep-Alive -----------------------------7d33a816d302b6 
Content-Disposition:form-data; 
name="userfile1"; 
filename="E:\s"Content-Type: 
application/octet-stream abbXXXccc 
-----------------------------7d33a816d302b6 

Content-Disposition: form-data; 

name="text1" foo 

-----------------------------7d33a816d302b6 <这里分割线多了两个->

Content-Disposition: form-data; 

name="password1" bar 

-----------------------------7d33a816d302b6--  <这里分割线的前端和末尾多了两个-,表明数据的结束>

大概架构就是这样,下面直接上一个测试代码:

POST_BOUNDS 是我顶一个一个字符串 可以定义你喜欢的任意值

复制代码

 1 -(void)multiPartPost:(NSDictionary *)dicData{
 2     
 3 
 4     NSURL *url = [NSURL URLWithString:@"http://192.168.1.112:8080/TestSerlvet/interfaces"];
 5     NSMutableString *bodyContent = [NSMutableString string];
 6     for(NSString *key in dicData.allKeys){
 7         id value = [dicData objectForKey:key];
 8         [bodyContent appendFormat:@"--%@\r\n",POST_BOUNDS];
 9         [bodyContent appendFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",key];
10         [bodyContent appendFormat:@"%@\r\n",value];
11     }
12     [bodyContent appendFormat:@"--%@--\r\n",POST_BOUNDS];
13     NSData *bodyData=[bodyContent dataUsingEncoding:NSUTF8StringEncoding];
14     NSMutableURLRequest *request  = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
15     [request addValue:[NSString stringWithFormat:@"multipart/form-data;boundary=%@",POST_BOUNDS] forHTTPHeaderField:@"Content-Type"];
16     [request addValue: [NSString stringWithFormat:@"%zd",bodyData.length] forHTTPHeaderField:@"Content-Length"];
17     [request setHTTPMethod:@"POST"];
18     [request setHTTPBody:bodyData];
19     NSLog(@"请求的长度%@",[NSString stringWithFormat:@"%zd",bodyData.length]);
20     __autoreleasing NSError *error=nil;
21     __autoreleasing NSURLResponse *response=nil;
22     NSLog(@"输出Bdoy中的内容>>\n%@",[[NSString alloc]initWithData:bodyData encoding:NSUTF8StringEncoding]);
23     NSData *reciveData= [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
24     if(error){
25         NSLog(@"出现异常%@",error);
26     }else{
27         NSHTTPURLResponse *httpResponse=(NSHTTPURLResponse *)response;
28         if(httpResponse.statusCode==200){
29             NSLog(@"服务器成功响应!>>%@",[[NSString alloc]initWithData:reciveData encoding:NSUTF8StringEncoding]);
30             
31         }else{
32             NSLog(@"服务器返回失败>>%@",[[NSString alloc]initWithData:reciveData encoding:NSUTF8StringEncoding]);
33             
34         }
35         
36     }
37 }

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值