unitywebrequest post有几种用法
时间: 2023-12-22 16:04:06 浏览: 85
UnityWebRequest.Post有两种常见的用法:
1. 使用URL和表单数据:
```csharp
string url = "https://example.com/api";
WWWForm form = new WWWForm();
form.AddField("key1", "value1");
form.AddField("key2", "value2");
UnityWebRequest request = UnityWebRequest.Post(url, form);
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success) {
// 请求成功处理逻辑
} else {
// 请求失败处理逻辑
}
```
2. 使用URL和JSON数据:
```csharp
string url = "https://example.com/api";
string jsonData = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
byte[] postData = System.Text.Encoding.UTF8.GetBytes(jsonData);
UnityWebRequest request = UnityWebRequest.Post(url, "POST");
request.uploadHandler = new UploadHandlerRaw(postData);
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success) {
// 请求成功处理逻辑
} else {
// 请求失败处理逻辑
}
```
这两种用法分别适用于传输表单数据和JSON数据的情况。你可以根据实际需求选择其中一种来使用。
阅读全文