我需要发布到此URL:api.platform/media.我真的是HTTP请求的新手,我需要发送一个包含图像和其他3个参数的请求.我有我需要的值,但我不知道从哪里开始或如何工作....标头,Content-Length和其他一些有线值.
I need to post to this url: api.platform/media. I am really new to HTTP request and I need to send a request that contains an image and 3 other parameters. I have the values that I need but I don't know where to begin or how it works.... Headers, Content-Length and some other wired values.
这些是值:
media[user_profile_id] media[channels_list][] media[file]推荐答案
创建NSURLRequest的实例.将method属性设置为POST,然后设置正文数据.
Create an instance of NSURLRequest. Set the method property to POST, and set the body data.
数据必须是NSData的单个实例,因此您必须继续添加要发送的任何数据,每个字段都用&符号进行标记和分隔,如下所示:
The data needs to be a single instance of NSData, so you will have to keep appending whatever data you want to send, each field labeled and separated by ampersands like so:
id = 12345& channels = 1,2,3& image = 123abcdef
id=12345&channels=1,2,3&image=123abcdef
请记住,您需要使用NSString的dataUsingEncoding:方法将所有字符串转换为二进制.如果您使用UIImage作为图像,则它具有类似的方法.
Remember, you need to transform all strings to binary, using NSString's dataUsingEncoding: method. If you are using UIImage for the image, it has a similar method.
服务器当然需要知道如何解析数据,字符串是字符串,图像是图像.
The server of course needs to know how to parse the data, strings being strings and the image being an image.
最后,创建一个NSURLConnection实例,将当前对象设置为委托,并实现委托协议以接收响应.
Finally, create an instance of NSURLConnection, set your current object as delegate and implement the delegate protocol to receive the response.
更多推荐
HTTP"POST"在iOS中要求
发布评论