Sending HTTP requests in Unity using C#
Unity provides customizable networking capabilities that allow developers to efficiently exchange data over the Internet. This guide will provide a comprehensive overview of how to send HTTP requests in Unity using C# and UnityWebRequest (Unity's current networking API), covering GET and POST operations.
GET request
GET request is used to retrieve data from the server. To send a GET request using UnityWebRequest, use the following code:
<code class="language-csharp">IEnumerator GetRequest(string uri) { UnityWebRequest uwr = UnityWebRequest.Get(uri); yield return uwr.SendWebRequest(); if (uwr.isNetworkError) { Debug.Log("发送错误: " + uwr.error); } else { Debug.Log("接收: " + uwr.downloadHandler.text); } }</code>
POST request
POST request is used to send data to the server. You can send form data and JSON data.
POST request using form data
<code class="language-csharp">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("发送错误: " + uwr.error); } else { Debug.Log("接收: " + uwr.downloadHandler.text); } }</code>
POST request using JSON data
<code class="language-csharp">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"); yield return uwr.SendWebRequest(); if (uwr.isNetworkError) { Debug.Log("发送错误: " + uwr.error); } else { Debug.Log("接收: " + uwr.downloadHandler.text); } }</code>
POST request using multi-part form data/multi-part form file
<code class="language-csharp">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("发送错误: " + uwr.error); } else { Debug.Log("接收: " + uwr.downloadHandler.text); } }</code>
PUT request
<code class="language-csharp">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("发送错误: " + uwr.error); } else { Debug.Log("接收: " + uwr.downloadHandler.text); } }</code>
DELETE request
<code class="language-csharp">IEnumerator DeleteRequest(string url) { UnityWebRequest uwr = UnityWebRequest.Delete(url); yield return uwr.SendWebRequest(); if (uwr.isNetworkError) { Debug.Log("发送错误: " + uwr.error); } else { Debug.Log("已删除"); } }</code>
The above is the detailed content of How to Send HTTP GET, POST, PUT, and DELETE Requests in Unity using C#?. For more information, please follow other related articles on the PHP Chinese website!