// 构造请求 UnityWebRequest request = new UnityWebRequest(url, "POST"); byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(json); request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw); request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer(); request.SetRequestHeader("Content-Type", "application/json"); // 发送请求 yield return request.SendWebRequest(); // 处理返回结果 if (request.isNetworkError) { Debug.Log(request.error); resultText.text = request.error; }替换掉result,unity版本太老,总是报错
时间: 2024-03-27 18:39:49 浏览: 176
C# 实现HTTPS协议POST数据到接口.rar
如果您的 Unity 版本太老,无法使用 Result 枚举类型,您可以使用以下方式替换代码中的 `request.isNetworkError`:
```
// 构造请求
UnityWebRequest request = new UnityWebRequest(url, "POST");
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(json);
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
// 发送请求
yield return request.Send();
// 处理返回结果
if (request.isError)
{
Debug.Log(request.error);
resultText.text = request.error;
}
```
这段代码中使用了 `request.isError` 来判断是否发生网络错误。如果 `isError` 为 true,则表示发生了网络错误,可以通过 `request.error` 获取错误信息。
阅读全文