Unity UnityWebRequest 简单post get 请求

这里写自定义目录标题

UnityWebRequest 简单post get 请求

简单的请求,有需要可拓展

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.Text;
using System;
using LitJson;
using SadTools;
using Http.Model;
using UnityEditor.PackageManager.Requests;

public class HttpClientVR : UnitySingleton<HttpClientVR>
{
    private static string TAG = "[HttpClient] ";
    //服务器地址
    private static string url = "https://**************/server/";
    private void Start()
    {

    }

    /// <summary>
    /// Post请求
    /// </summary>
    /// <param name="req">请求体</param>
    /// <param name="callback">接口回调</param>
    public void Post(BaseRequestModel req, Action<string> callback)
    {
        StartCoroutine(I_RequestByJsonBodyPost(url + req.type, JsonMapper.ToJson(req), callback));
    }

    /// <summary>
    /// 接口请求携程
    /// </summary>
    private static IEnumerator I_RequestByJsonBodyPost(string url, string json, Action<string> callback)
    {
        Debug.Log(TAG + url + "\n" + "RequestModel : " + 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.certificateHandler = new WebRequestCertificate();
        uwr.SetRequestHeader("Content-Type", "application/json");
        yield return uwr.SendWebRequest();
        if (uwr.isDone)
        {
            Debug.Log(uwr.downloadHandler.text);
            string data = uwr.downloadHandler.text;
            if (data != null && !data.Equals(""))
            {
                callback(data);
            }
            else
            {
                Debug.LogError("接口请求失败");
            }

        }
    }

    /// <summary>
    /// Get请求
    /// </summary>
    /// <param name="url">请求地址</param>
    /// <param name="callback">接口回调</param>
    public void Get(string url, Action<string> callback)
    {
        StartCoroutine(I_RequestGET(url, callback));
    }

    /// <summary>
    /// 接口请求携程
    /// </summary>
    private static IEnumerator I_RequestGET(string url, Action<string> callback)
    {
        UnityWebRequest request = UnityWebRequest.Get(url);
        request.certificateHandler = new WebRequestCertificate();
        yield return request.SendWebRequest();
        Debug.Log(request.error);
        Debug.Log(request.responseCode);
        Debug.Log(request.downloadHandler.text);
        callback?.Invoke(request.downloadHandler.text);
    }

}

### 回答1: UnityWebRequest.PostUnity中的一个类,用于向服务器发送POST请求。它可以用于向服务器发送数据,例如表单数据、JSON数据等。使用UnityWebRequest.Post可以方便地与服务器进行通信,并获取服务器返回的数据。 ### 回答2: UnityWebrequestUnity引擎提供的一个用于网络通信的类库,其中post是其中最常用的方法之一。它是通过HTTP协议向服务器提交数据的一种方式,相比于get方法更加安全可靠。 在使用post方法时,需要向服务器发送一段特定格式的数据,这段数据包含了要向服务器传递的参数以及参数的值。这些数据需要以键值对的方式组织,并使用特定的分隔符进行连接。在 UnityWebrequest.post 方法中,参数需要通过一个Dictionary<string,string>类型的参数进行传递。例如: Dictionary<string, string> postParams = new Dictionary<string, string>(); postParams["username"] = "user1"; postParams["password"] = "password1"; UnityWebRequest www = UnityWebRequest.Post("http://www.example.com/login", postParams); yield return www.SendWebRequest(); if (www.isNetworkError || www.isHttpError) { Debug.LogError(www.error); } else { Debug.Log("Form upload complete!"); } 在上述示例中,向 http://www.example.com/login 发送了一个POST请求。参数使用了一个Dictionary<string,string>对象,其中包含了用户名和密码两个参数及其对应的值。在发送请求后,我们需要等待服务器的响应结果。如果请求成功,我们可以获取服务器返回的数据并进行处理。如果请求失败,则会抛出 NetworkError或 HttpError 异常,可以根据具体的错误信息进行处理。 UnityWebrequest.postUnity引擎中一个十分强大、常用的API,具备广泛的适用性,在开发各种类型的游戏时都可以使用。 如果开发者想进行网络通信,就可以选择UnityWebrequest.post方法,实现安全、可靠、高效的数据传输。 ### 回答3: UnityWebRequest.postUnity中用于在Http协议下向远程服务器发送POST请求的API。它可以用于向服务器传递表单数据、上传文件和进行基于HTTP协议的远程调用等操作。 通过UnityWebRequest.post方法可以设置请求的URL、请求的体、请求头、超时时间等请求参数。在发送请求之后可以通过异步的方式获取响应结果,通过设置回调函数处理获得的响应结果。在处理请求的过程中,还可以根据需要设置请求的超时时间、POST方式、上传内容和返回格式等操作。 UnityWebRequest.post能够满足我们许多实际开发中的需求,比如向服务器上传文件、进行后台数据请求等。此外,UnityWebRequest.post还具备多线程操作等多种优点,可以提高网络请求的效率。 总之,UnityWebRequest.postUnity中用于进行Http协议下的POST请求的API,它可以完成许多网络请求的操作。了解和掌握这个API是Unity网络开发中的基本技能,也是开发者必不可少的技能之一。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值