如何使用Unity在C#中发送HTTP GET和POST请求?
我想要的是:
我尝试过的
大多数问题与线程有关,我在C#中经验不足。我使用的IDE是Intellij Rider。
该WWWAPI应该得到这个工作,但UnityWebRequest取而代之,所以我会回答新的API。真的很简单。您必须使用协程通过Unity的API来执行此操作,否则必须使用C#标准Web请求API和Thread之一。使用协程,您可以发出请求直到完成。这不会阻止主线程,也不会阻止其他脚本运行。
WWW
UnityWebRequest
注意事项 :
对于以下示例,如果您使用的是 Unity 2017.2 以下的 版本 ,请替换SendWebRequest()为Send(),然后替换isNetworkError为isError。这将适用于较低版本的Unity。另外,如果您需要以二进制形式访问下载的数据,请替换uwr.downloadHandler.text为uwr.downloadHandler.data。最后,该SetRequestHeader函数用于设置请求的标头。
SendWebRequest()
Send()
isNetworkError
isError
uwr.downloadHandler.text
uwr.downloadHandler.data
SetRequestHeader
GET请求 :
void Start() { StartCoroutine(getRequest("http:///www.yoururl.com")); } IEnumerator GetRequest(string uri) { UnityWebRequest uwr = UnityWebRequest.Get(uri); yield return uwr.SendWebRequest(); if (uwr.isNetworkError) { Debug.Log("Error While Sending: " + uwr.error); } else { Debug.Log("Received: " + uwr.downloadHandler.text); } }
带表单的POST请求 :
void Start() { StartCoroutine(postRequest("http:///www.yoururl.com")); } IEnumerator PostRequest(string url) { WWWForm form = new WWWForm(); form.AddField("myField", "myData"); form.AddField("Game Name", "Mario Kart"); UnityWebRequest uwr = UnityWebRequest.Post(url, form); yield return uwr.SendWebRequest(); if (uwr.isNetworkError) { Debug.Log("Error While Sending: " + uwr.error); } else { Debug.Log("Received: " + uwr.downloadHandler.text); } }
与Json的POST请求 :
void Start() { StartCoroutine(postRequest("http:///www.yoururl.com", "your json")); } IEnumerator PostRequest(string url, string json) { var uwr = new UnityWebRequest(url, "POST"); byte[] jsonToSend = new System.Text.UTF8Encoding().GetBytes(json); uwr.uploadHandler = (UploadHandler)new UploadHandlerRaw(jsonToSend); uwr.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer(); uwr.SetRequestHeader("Content-Type", "application/json"); //Send the request then wait here until it returns yield return uwr.SendWebRequest(); if (uwr.isNetworkError) { Debug.Log("Error While Sending: " + uwr.error); } else { Debug.Log("Received: " + uwr.downloadHandler.text); } }
带有Multipart FormData / Multipart表单文件的POST请求 :
void Start() { StartCoroutine(postRequest("http:///www.yoururl.com")); } IEnumerator PostRequest(string url) { List<IMultipartFormSection> formData = new List<IMultipartFormSection>(); formData.Add(new MultipartFormDataSection("field1=foo&field2=bar")); formData.Add(new MultipartFormFileSection("my file data", "myfile.txt")); UnityWebRequest uwr = UnityWebRequest.Post(url, formData); yield return uwr.SendWebRequest(); if (uwr.isNetworkError) { Debug.Log("Error While Sending: " + uwr.error); } else { Debug.Log("Received: " + uwr.downloadHandler.text); } }
放置请求 :
void Start() { StartCoroutine(putRequest("http:///www.yoururl.com")); } IEnumerator PutRequest(string url) { byte[] dataToPut = System.Text.Encoding.UTF8.GetBytes("Hello, This is a test"); UnityWebRequest uwr = UnityWebRequest.Put(url, dataToPut); yield return uwr.SendWebRequest(); if (uwr.isNetworkError) { Debug.Log("Error While Sending: " + uwr.error); } else { Debug.Log("Received: " + uwr.downloadHandler.text); } }
删除请求 :
void Start() { StartCoroutine(deleteRequest("http:///www.yoururl.com")); } IEnumerator DeleteRequest(string url) { UnityWebRequest uwr = UnityWebRequest.Delete(url); yield return uwr.SendWebRequest(); if (uwr.isNetworkError) { Debug.Log("Error While Sending: " + uwr.error); } else { Debug.Log("Deleted"); } }