Unity UnityWebRequest 进行Get和Post请求

using System.Collections;
using UnityEngine;
using UnityEngine.Networking;

public class NetWorkHttp : MonoBehaviour
{
    private static NetWorkHttp instance;
    public static NetWorkHttp Instance
    {
        get
        {
            if (instance == null)
            {
                GameObject go = new GameObject("NetWorkHttp");
                DontDestroyOnLoad(go);
                instance = go.CreateOrAddComponent<NetWorkHttp>();
            }
            return instance;
        }
    }

    public void Get(string url)
    {
        UnityWebRequest unityWebRequest = UnityWebRequest.Get(url);
        StartCoroutine(GetUrl(unityWebRequest));
    }

    private IEnumerator GetUrl(UnityWebRequest unityWebRequest)
    {
        yield return unityWebRequest.SendWebRequest();
        if (unityWebRequest.isHttpError || unityWebRequest.isNetworkError)
            Debug.Log(unityWebRequest.error);
        else
        {
            Debug.Log(unityWebRequest.downloadHandler.text);
            AccountEntity entity = JsonUtility.FromJson<AccountEntity>(unityWebRequest.downloadHandler.text);
            Debug.Log(entity.pwd);
        }
    }
    private IEnumerator GetPost(UnityWebRequest unityWebRequest)
    {
        yield return unityWebRequest.SendWebRequest();
        if (unityWebRequest.isHttpError || unityWebRequest.isNetworkError)
            Debug.Log(unityWebRequest.error);
        else
        {
            Debug.Log(unityWebRequest.downloadHandler.text);
        }
    }

    public void Post(string url)
    {
        WWWForm wWWForm = new WWWForm();
        AccountEntity entity = new AccountEntity { account = "zzzsss123", pwd = "sssss123" };
        Debug.Log(JsonUtility.ToJson(entity));
        wWWForm.AddField("", JsonUtility.ToJson(entity));
        UnityWebRequest unityWebRequest = UnityWebRequest.Post(url,wWWForm);
        StartCoroutine(GetPost(unityWebRequest));
    }
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
UnityWebRequestUnity 引擎提供的一种用于发送网络请求的类,它支持多种协议,包括 HTTP、HTTPS、FTP、File 等。下面是一个简单的例子,演示如何使用 UnityWebRequest 发送一个 GET 请求: ```csharp IEnumerator GetRequest(string url) { using (UnityWebRequest webRequest = UnityWebRequest.Get(url)) { yield return webRequest.SendWebRequest(); if (webRequest.result == UnityWebRequest.Result.ConnectionError || webRequest.result == UnityWebRequest.Result.ProtocolError) { Debug.LogError("Error: " + webRequest.error); } else { Debug.Log("Response: " + webRequest.downloadHandler.text); } } } ``` 这里使用了 IEnumerator 和 yield 关键字,因为 UnityWebRequest 是异步执行的,需要使用协程来处理。 使用 UnityWebRequest 发送 POST 请求也很简单,只需要将上面的 GetRequest 方法修改为: ```csharp IEnumerator PostRequest(string url, string data) { byte[] bodyRaw = Encoding.UTF8.GetBytes(data); using (UnityWebRequest webRequest = UnityWebRequest.Post(url, "POST")) { webRequest.uploadHandler = new UploadHandlerRaw(bodyRaw); webRequest.downloadHandler = new DownloadHandlerBuffer(); webRequest.SetRequestHeader("Content-Type", "application/json"); yield return webRequest.SendWebRequest(); if (webRequest.result == UnityWebRequest.Result.ConnectionError || webRequest.result == UnityWebRequest.Result.ProtocolError) { Debug.LogError("Error: " + webRequest.error); } else { Debug.Log("Response: " + webRequest.downloadHandler.text); } } } ``` 这里使用了 UploadHandlerRaw 将请求体转换为 byte 数组,并设置了 Content-Type 为 application/json。同时使用 DownloadHandlerBuffer 接收服务器响应的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值