我想使用来自统一 2107.4 的 UnityWebRequest 从我的服务器发送和获取数据
我尝试使用此处的代码
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
public class WebRequestExample : MonoBehaviour
{
// Where to send our request
const string DEFAULT_URL = "https://jsonplaceholder.typicode.com/todos/1";
string targetUrl = DEFAULT_URL;
// Keep track of what we got back
string recentData = "";
void Awake()
{
this.StartCoroutine(this.RequestRoutine(this.targetUrl, this.ResponseCallback));
}
// Web requests are typially done asynchronously, so Unity's web request system
// returns a yield instruction while it waits for the response.
//
private IEnumerator RequestRoutine(string url, Action<string> callback = null)
{
// Using the static constructor
var request = UnityWebRequest.Get(url);
// Wait for the response and then get our data
yield return request.SendWebRequest();
var data = request.downloadHandler.text;
// This isn't required, but I prefer to pass in a callback so that I can
// act on the response data outside of this function
if (callback != null)
callback(data);
}
// Callback to act on our response data
private void ResponseCallback(string data)
{
Debug.Log(data);
recentData = data;
}
// Old fashioned GUI system to show the example
void OnGUI()
{
this.targetUrl = GUI.TextArea(new Rect(0, 0, 500, 100), this.targetUrl);
GUI.TextArea(new Rect(0, 100, 500, 300), this.recentData);
if (GUI.Button(new Rect(0, 400, 500, 100), "Resend Request"))
{
this.StartCoroutine(this.RequestRoutine(targetUrl, this.ResponseCallback));
}
}
}
和cs_getannounce,cs_login,cs_zonelist是我试图访问的链接
它应该通过提供的所有三个链接获取数据,但只有 cs_getannounce 和 cs_login 可以,而 cs_zonelist 不能(得到错误 403)。我也尝试使用 unity 2018,但没有运气。当我使用统一 2019 时,它可以工作。但是我真的要用2017