Unity Http Post请求之Json参数
使用Litjson
	void Start()
    {
        //使用litJson创建json格式的参数数据
        JsonData data = new JsonData();
        data["与后端协商好的参数名"] = "你要写入的参数";
        byte[] postBytes = System.Text.Encoding.Default.GetBytes(data.ToJson());
        //使用原生UnityWebRequest(推荐)
        StartCoroutine(UnityWebRequestPost("你的url", postBytes));
        //使用UniRx
        ObservableWWW.Post("你的url", postBytes, new Dictionary<string, string>() { { "Content-Type", "application/json" } })
                     .Subscribe(result => Debug.Log(result));
    }
    IEnumerator UnityWebRequestPost(string url, byte[] postBytes)
    {
        UnityWebRequest request = new UnityWebRequest(url, "POST");
        request.uploadHandler = (UploadHandler)new UploadHandlerRaw(postBytes);
        request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
        request.SetRequestHeader("Content-Type", "application/json");
        yield return request.SendWebRequest();
        Debug.Log("Status Code: " + request.responseCode);
        if (request.isNetworkError || request.isHttpError)
        {
            Debug.LogError(request.error);
        }
        else
        {
            Debug.Log(request.downloadHandler.text);
        }
    }
声明:文章内容整理来源于网络,版权属于原作者,如有问题,请联系我们!
	来源:https://www.cnblogs.com/unity3ds/p/12680239.html
- 上一篇:UNIT3D更改界面字体大小颜色 2021/5/5
- 下一篇:Qt QSerialPort 类实现串口通信 2021/4/23


