Home > Backend Development > C++ > How to Send HTTP GET, POST, PUT, and DELETE Requests in Unity using C#?

How to Send HTTP GET, POST, PUT, and DELETE Requests in Unity using C#?

Patricia Arquette
Release: 2025-01-19 21:21:12
Original
880 people have browsed it

How to Send HTTP GET, POST, PUT, and DELETE Requests in Unity using C#?

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template