博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2. ASIHttpRequest-发送数据
阅读量:6886 次
发布时间:2019-06-27

本文共 1737 字,大约阅读时间需要 5 分钟。

hot3.png

发送数据

设定request头

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];[request addRequestHeader:@"Referer" value:@"http://www.dreamingwish.com/"];

使用ASIFormDataRequest POST表单

通常数据是以’application/x-www-form-urlencoded’格式发送的,如果上传了二进制数据或者文件,那么格式将自动变为‘multipart/form-data’ 

文件中的数据是需要时才从磁盘加载,所以只要web server能处理,那么上传大文件是没有问题的。

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];[request setPostValue:@"Ben" forKey:@"first_name"];[request setPostValue:@"Copsey" forKey:@"last_name"];[request setFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photo"];

数据的mime头是自动判定的,但是如果你想自定义mime头,那么这样:

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];// Upload a file on disk[request setFile:@"/Users/ben/Desktop/ben.jpg" withFileName:@"myphoto.jpg" andContentType:@"image/jpeg"forKey:@"photo"];// Upload an NSData instance[request setData:imageData withFileName:@"myphoto.jpg" andContentType:@"image/jpeg" forKey:@"photo"];

你可以使用addPostValue方法来发送相同name的多个数据(梦维:服务端会以数组方式呈现):

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];[request addPostValue:@"Ben" forKey:@"names"];[request addPostValue:@"George" forKey:@"names"];[request addFile:@"/Users/ben/Desktop/ben.jpg" forKey:@"photos"];[request addData:imageData withFileName:@"george.jpg" andContentType:@"image/jpeg" forKey:@"photos"];

PUT请求、自定义POST请求

如果你想发送PUT请求,或者你想自定义POST请求,使用appendPostData: 或者 appendPostDataFromFile:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];[request appendPostData:[@"This is my data" dataUsingEncoding:NSUTF8StringEncoding]];// Default becomes POST when you use appendPostData: / appendPostDataFromFile: / setPostBody:[request setRequestMethod:@"PUT"];

转载于:https://my.oschina.net/u/874588/blog/101597

你可能感兴趣的文章
深夜,想到今天学的linux内容,太值了
查看>>
Thread类常用方法
查看>>
/etc/resolv.conf中内容被清空的解决办法
查看>>
Yarn大体框架和工作流程研究
查看>>
vue学习笔记(一)
查看>>
微软专家推荐11个Chrome 插件
查看>>
三天学会HTML5——SVG和Canvas的使用
查看>>
Azure 部署 Asp.NET Core Web App
查看>>
MySql基本操作(二)
查看>>
SaaS、PaaS和云计算 搅动未来软件发展
查看>>
App测试点
查看>>
运行jar包
查看>>
supervisor centos安装
查看>>
深入分析:Flash vs. HTML5 網路影音格式落誰家?
查看>>
带你领略Linux系统发展及版本更迭
查看>>
聊聊ES7与ES8特性
查看>>
尾调用
查看>>
Redis学习笔记
查看>>
Version 1.6.0 of the JVM is not suitable for the this product.Version:1.8 or greater is required
查看>>
《JAVA与模式》之责任链模式
查看>>