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
Susan SarandonOriginal
2025-01-19 21:01:12818browse

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:

<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 containing form data

For posting form data, use WWWForm.

<code class="language-csharp">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);
    }
}</code>

POST request containing JSON data

To send a POST request containing 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>

PUT and DELETE requests

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

PUT request

<code class="language-csharp">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);
    }
}</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>

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:
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