HTTP"POST"在iOS中要求

编程入门 行业动态 更新时间:2024-10-28 08:15:49
本文介绍了HTTP"POST"在iOS中要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要发布到此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中要求

本文发布于:2023-10-12 00:28:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483269.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:quot   HTTP   iOS   POST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!