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

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

Susan Sarandon
Release: 2025-01-19 21:01:12
Original
890 people have browsed it

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

Send HTTP request using C# in Unity

In Unity development, sending HTTP requests is crucial for interacting with network services and transmitting data. This article will comprehensively guide you on how to send GET and POST requests in Unity's C#.

GET request in Unity

To perform a GET request, you can use UnityWebRequest.Get. The code is as follows:

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);
    }
}
Copy after login

POST request containing form data

For posting form data, use WWWForm.

IEnumerator postRequest(string url)
{
    WWWForm form = new WWWForm();
    form.AddField("myField", "myData");
    form.AddField("游戏名称", "马里奥赛车");

    UnityWebRequest uwr = UnityWebRequest.Post(url, form);
    yield return uwr.SendWebRequest();

    if (uwr.isNetworkError)
    {
        Debug.Log("发送错误: " + uwr.error);
    }
    else
    {
        Debug.Log("接收: " + uwr.downloadHandler.text);
    }
}
Copy after login

POST request containing JSON data

To send a POST request containing JSON data:

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);
    }
}
Copy after login

PUT and DELETE requests

You can also use UnityWebRequest to perform PUT and DELETE requests as follows:

PUT request

IEnumerator putRequest(string url)
{
    byte[] dataToPut = System.Text.Encoding.UTF8.GetBytes("你好,这是一个测试");
    UnityWebRequest uwr = UnityWebRequest.Put(url, dataToPut);
    yield return uwr.SendWebRequest();

    if (uwr.isNetworkError)
    {
        Debug.Log("发送错误: " + uwr.error);
    }
    else
    {
        Debug.Log("接收: " + uwr.downloadHandler.text);
    }
}
Copy after login

DELETE request

IEnumerator deleteRequest(string url)
{
    UnityWebRequest uwr = UnityWebRequest.Delete(url);
    yield return uwr.SendWebRequest();

    if (uwr.isNetworkError)
    {
        Debug.Log("发送错误: " + uwr.error);
    }
    else
    {
        Debug.Log("已删除");
    }
}
Copy after login

These code snippets demonstrate how to send HTTP requests asynchronously in Unity and handle responses efficiently. Leverage Unity's coroutine system to prevent blocking the main thread and ensure smooth game performance.

The above is the detailed content of How to Make HTTP GET, POST, PUT, and DELETE Requests in Unity using C#?. For more information, please follow other related articles on the PHP Chinese website!

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