zoukankan      html  css  js  c++  java
  • 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);
            }
        }
  • 相关阅读:
    关于struts页面跳转的问题
    java中==和equals的区别
    控制广播风暴的方法
    广播风暴的成因及解决办法
    思科FEX配置
    思科vPC技术和配置
    数据中心架构TOR和EOR
    ARP表项及老化时间
    MAC地址表和老化时间
    track 3 list boolean or
  • 原文地址:https://www.cnblogs.com/unity3ds/p/12680239.html
Copyright © 2011-2022 走看看