uri | The target URI to which the string will be transmitted. |
postData | Form body data. Will be converted to UTF-8 string. |
contentType | Value for the Content-Type header, for example application/json. |
UnityWebRequest
A UnityWebRequest configured to send string to uri
via POST
.
创建一个经配置可通过 HTTP POST
向服务器发送表单数据的 UnityWebRequest。
This method creates a UnityWebRequest, sets the url
to the string uri
argument and sets the method
to POST
. The Content-Type
header will be set to contentType
.
此方法将一个 DownloadHandlerBuffer 附加到 UnityWebRequest。这是为方便起见,因为我们预计大部分用户将使用 DownloadHandler 检查服务器的回复,尤其是对于 REST API 来说。
The data in postData
will be interpreted into a byte stream via System.Text.Encoding.UTF8. The resulting byte stream will be stored in an UploadHandlerRaw and the Upload Handler will be attached to this UnityWebRequest.
using UnityEngine; using UnityEngine.Networking; using System.Collections;
public class MyBehavior : MonoBehaviour { void Start() { StartCoroutine(Upload()); }
IEnumerator Upload() { using (UnityWebRequest www = UnityWebRequest.Post("https://www.my-server.com/myapi", "{ \"field1\": 1, \"field2\": 2 }", "application/json")) { yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success) { Debug.Log(www.error); } else { Debug.Log("Form upload complete!"); } } } }
uri | 要向其传输表单数据的目标 URI。 |
formData | 封装在 WWWForm 对象中的表单字段或文件,用于格式化并传输到远程服务器。 |
UnityWebRequest
经配置可通过 POST
向 uri
发送表单数据的 UnityWebRequest。
创建一个经配置可通过 HTTP POST
向服务器发送表单数据的 UnityWebRequest。
此方法可创建一个 UnityWebRequest,将 url
设置为字符串 uri
参数,并将 method
设置为 POST
。Content-Type
标头将从 formData
参数中复制。
此方法将一个 DownloadHandlerBuffer 附加到 UnityWebRequest。这是为方便起见,因为我们预计大部分用户将使用 DownloadHandler 检查服务器的回复,尤其是对于 REST API 来说。formData
对象将根据其内容生成适当格式化的字节流。生成的字节流将存储在 UploadHandlerRaw 中,且上传处理程序将附加到此 UnityWebRequest。
using UnityEngine; using UnityEngine.Networking; using System.Collections;
public class MyBehavior2 : MonoBehaviour { void Start() { StartCoroutine(Upload()); }
IEnumerator Upload() { WWWForm form = new WWWForm(); form.AddField("myField", "myData");
UnityWebRequest www = UnityWebRequest.Post("https://www.my-server.com/myform", form); yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success) { Debug.Log(www.error); } else { Debug.Log("Form upload complete!"); } } }
uri | 要向其传输表单数据的目标 URI。 |
multipartFormSections | 要进行格式化并传输到远程服务器的表单字段或文件的列表。 |
boundary | 唯一边界字符串,将在多部分表单中分隔表单字段时使用。如果未提供,系统将为您生成边界。 |
UnityWebRequest
经配置可通过 POST
向 uri
发送表单数据的 UnityWebRequest。
创建一个经配置可通过 HTTP POST
向服务器发送表单数据的 UnityWebRequest。
此方法可创建一个 UnityWebRequest,将 url
设置为字符串 uri
参数,并将 method
设置为 POST
。Content-Type
标头将默认被设置为 multipart/form-data
,并带有适当的边界规范。
如果您提供自定义的 boundary
字节数组,请注意必须保证字节序列是唯一的且不能出现在表单数据主体中的任何位置。有关多部分表单和表单边界的更多信息,请参阅 RFC 2388。
此方法将一个 DownloadHandlerBuffer 附加到 UnityWebRequest。这是为方便起见,因为我们预计大部分用户将使用 DownloadHandler 检查服务器的回复,尤其是对于 REST API 来说。multipartFormSections
中的 IMultipartFormSection 对象列表将被格式化为一个有效的多部分表单主体。每个对象都将被解释为一个离散的表单部分。格式化此多部分表单主体所生成的字节流将存储在 UploadHandlerRaw 中并附加到此 UnityWebRequest。
**使用 IMultipartFormSection**
为了让您更好地控制您指定表单数据的方式,UnityWebReques 系统包含了一个可由用户实现的 IMultipartFormSection 界面。对于标准应用程序,Unity 还提供了数据和文件部分的默认实现。
另请参阅:MultipartFormDataSection 和 MultipartFormFileSection。
可为此方法提供一个 IMultipartFormSection 对象列表。列表成员将被格式化为一个多部分表单,由 RFC 2388 定义。
多部分表单需要唯一边界字符串来定义字段之间的分隔。必须保证边界字符串未出现在请求中任何表单字段主体中的任何位置。如果您未提供边界,Unity 将生成一个边界。生成的边界是 40 个随机可打印字节,可有效避免与表单字段数据发生冲突。但是,如果您的应用程序需要您提供自定义边界字符串,您可能需要进行自定义。
提供的边界(如果有)将自动从字节数组转换为 UTF8 字符。
using UnityEngine; using UnityEngine.Networking; using System.Collections;
public class MyBehavior3 : MonoBehaviour { void Start() { StartCoroutine(Upload()); }
IEnumerator Upload() { WWWForm form = new WWWForm(); form.AddField("myField", "myData");
UnityWebRequest www = UnityWebRequest.Post("https://www.my-server.com/myform", form); yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success) { Debug.Log(www.error); } else { Debug.Log("Form upload complete!"); } } }
uri | 要向其传输表单数据的目标 URI。 |
formFields | 用于表示表单字段的键和值的字符串。将自动格式化为经过 URL 编码的表单主体。 |
UnityWebRequest
经配置可通过 POST
向 uri
发送表单数据的 UnityWebRequest。
创建一个经配置可通过 HTTP POST
向服务器发送表单数据的 UnityWebRequest。
此方法可创建一个 UnityWebRequest,将 url
设置为字符串 uri
参数,并将 method
设置为 POST
。Content-Type
标头将被设置为 application/x-www-form-urlencoded
。formFields
中的字符串字典将被解释为一个表单字段列表,其字段 ID 是字典键,其字段值为字典值。键和值都将进行转义,然后加入到经过 URL 编码的表单字符串。(例如,key1=value1&key2=value2
)。
此方法默认将一个 DownloadHandlerBuffer 附加到 UnityWebRequest。这是为方便起见,因为我们预计大部分用户将使用 DownloadHandler 检查服务器的回复,尤其是对于 REST API 来说。
从 formFields
生成的经过 URL 编码的表单字符串将被转换为字节流,并存储在一个将附加到此 UnityWebRequest 的 UploadHandlerRaw 中。
using UnityEngine; using UnityEngine.Networking; using System.Collections;
public class MyBehavior4 : MonoBehaviour { void Start() { StartCoroutine(Upload()); }
IEnumerator Upload() { WWWForm form = new WWWForm(); form.AddField("myField", "myData");
UnityWebRequest www = UnityWebRequest.Post("https://www.my-server.com/myform", form); yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success) { Debug.Log(www.error); } else { Debug.Log("Form upload complete!"); } } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.