提供与 Web 服务器通信的方法。
UnityWebRequest
处理与 Web 服务器的 HTTP 通信流程。要下载和上传数据,请分别使用 DownloadHandler 和 UploadHandler。UnityWebRequest
包含用于返回针对常见用例配置的 UnityWebRequest
实例的静态实用程序函数。例如
要从 UnityWebRequest
实例发送 Web 请求,请调用 UnityWebRequest.SendWebRequest。在 UnityWebRequest
开始与远程服务器通信后,您无法更改该 UnityWebRequest
实例中的任何属性。支持 HTTPS,服务器证书将针对应用程序运行的系统上可用的根证书存储进行验证。验证可以被禁用(例如,对于使用自签名证书的开发服务器)或通过分配 UnityWebRequest.certificateHandler 属性更改为自定义处理。
根据您的应用程序运行的平台,UnityWebRequest
或者会自行设置 User-Agent 标头,或者将其留给操作系统设置。 UnityWebRequest
为除 iOS、Xbox 平台和 WebGL 之外的所有平台设置 User-Agent
标头。
注意:从 Unity 2019.2 开始,UnityWebRequest
会为 Android 设备设置 User-Agent
标头。在早期版本中,操作系统会设置 User-Agent
标头。
注意:如果应用程序运行的设备使用代理设置,则 UnityWebRequest
会在应用程序发送请求后应用代理设置。
kHttpVerbCREATE | 字符串 "CREATE",通常用作 HTTP CREATE 请求的动词。 |
kHttpVerbDELETE | 字符串 "DELETE",通常用作 HTTP DELETE 请求的动词。 |
kHttpVerbGET | 字符串 "GET",通常用作 HTTP GET 请求的动词。 |
kHttpVerbHEAD | 字符串 "HEAD",通常用作 HTTP HEAD 请求的动词。 |
kHttpVerbPOST | 字符串 "POST",通常用作 HTTP POST 请求的动词。 |
kHttpVerbPUT | 字符串 "PUT",通常用作 HTTP PUT 请求的动词。 |
certificateHandler | 保存对 CertificateHandler 对象的引用,该对象管理此 UnityWebRequest 的证书验证。 |
disposeCertificateHandlerOnDispose | 如果为 true,则附加到此 UnityWebRequest 的任何 CertificateHandler 都将在调用 UnityWebRequest.Dispose 时自动调用 CertificateHandler.Dispose。 |
disposeDownloadHandlerOnDispose | 如果为 true,则附加到此 UnityWebRequest 的任何 DownloadHandler 都将在调用 UnityWebRequest.Dispose 时自动调用 DownloadHandler.Dispose。 |
disposeUploadHandlerOnDispose | 如果为 true,则附加到此 UnityWebRequest 的任何 UploadHandler 都将在调用 UnityWebRequest.Dispose 时自动调用 UploadHandler.Dispose。 |
downloadedBytes | 返回系统从远程服务器下载的主体数据字节数。(只读) |
downloadHandler | 保存对 DownloadHandler 对象的引用,该对象管理此 UnityWebRequest 从远程服务器接收的主体数据。 |
downloadProgress | 返回一个介于 0.0 和 1.0 之间的浮点值,指示从服务器下载主体数据的进度。(只读) |
error | 一个可读的字符串,描述此 UnityWebRequest 对象在处理 HTTP 请求或响应时遇到的任何系统错误。默认值为 null。(只读) |
isDone | 在 UnityWebRequest 完成与远程服务器的通信后返回 true。(只读) |
isModifiable | 当 UnityWebRequest 的配置属性可以更改时返回 true。(只读) |
method | 定义此 UnityWebRequest 使用的 HTTP 动词,例如 GET 或 POST。 |
redirectLimit | 指示此 UnityWebRequest 在因“重定向限制超出”系统错误而停止之前所执行的重定向次数。 |
responseCode | 服务器返回的数字 HTTP 响应代码,例如 200、404 或 500。(只读) |
result | 此 UnityWebRequest 的结果。 |
timeout | 设置 UnityWebRequest 在超时秒数后尝试中止。 |
uploadedBytes | 返回系统上传到远程服务器的主体数据字节数。(只读) |
uploadHandler | 保存对 UploadHandler 对象的引用,该对象管理要上传到远程服务器的主体数据。 |
uploadProgress | 返回一个介于 0.0 和 1.0 之间的浮点值,指示向服务器上传主体数据的进度。 |
uri | 定义 UnityWebRequest 要与其通信的目标 URI。 |
url | 定义 UnityWebRequest 要与其通信的目标 URL。 |
useHttpContinue | 确定此 UnityWebRequest 是否将在其传出的请求标头中包含 Expect: 100-Continue。(默认值:true)。 |
UnityWebRequest | 创建使用默认选项且没有附加 DownloadHandler 或 UploadHandler 的 UnityWebRequest。默认方法是 GET。 |
Abort | 如果正在进行,则尽快停止 UnityWebRequest。 |
Dispose | 表示此 UnityWebRequest 不再使用,应清理其使用的任何资源。 |
GetRequestHeader | 检索自定义请求标头的值。 |
GetResponseHeader | 检索从收到的最新 HTTP 响应中接收到的响应标头的值。 |
GetResponseHeaders | 检索一个字典,其中包含此 UnityWebRequest 在最新 HTTP 响应中收到的所有响应标头。 |
SendWebRequest | 开始与远程服务器通信。 |
SetRequestHeader | 将 HTTP 请求标头设置为自定义值。 |
ClearCookieCache | 从缓存中清除存储的 cookie。 |
Delete | 创建为 HTTP DELETE 配置的 UnityWebRequest。 |
EscapeURL | 转义字符串中的字符,以确保它们是 URL 友好的。 |
GenerateBoundary | 生成一个随机的 40 字节数组,用作多部分表单边界。 |
Get | 创建用于 HTTP GET 的 UnityWebRequest。 |
Head | 创建配置为发送 HTTP HEAD 请求的 UnityWebRequest。 |
Post | 创建配置为通过 HTTP POST 将表单数据发送到服务器的 UnityWebRequest。 |
PostWwwForm | 创建配置为通过 HTTP POST 将表单数据发送到服务器的 UnityWebRequest。 |
Put | 创建配置为通过 HTTP PUT 将原始数据上传到远程服务器的 UnityWebRequest。 |
SerializeFormSections | 将 IMultipartFormSection 对象列表转换为包含原始多部分表单数据的字节数组。 |
SerializeSimpleForm | 将字符串字典序列化为包含 URL 编码 UTF8 字符的字节数组。 |
UnEscapeURL | 将 URL 友好的转义序列转换回普通文本。 |
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.