unity通过WWW POST请求数据,传入JSON格式参数;

注意:需要设备类型  headers["Content-Type"] = "application/json";

     private string jsonDataPost;//需要赋值的具体JSON格式的数据

    IEnumerator CallPost()
    {

        Dictionary<string, string> headers = new Dictionary<string, string>();
        headers["Content-Type"] = "application/json";
        //将文本转为byte数组  
        byte[] bs = System.Text.UTF8Encoding.UTF8.GetBytes(jsonDataPost);
        Debug.Log("JSON数据:" + bs);
        //向HTTP服务器提交Post数据  
        WWW www = new WWW(login_url, bs, headers);

        //等待服务器的响应  
        yield return www;

        //如果出现错误  
        if (www.error != null)
        {
            //获取服务器的错误信息  
            m_info = www.error;
            yield return null;
        }
        else
        {
            TokenDataInfo tokenDataInfo = JsonMapper.ToObject<TokenDataInfo>(www.text);

            authorization = tokenDataInfo.token_type + " " + tokenDataInfo.access_token;
            Debug.Log(authorization);
        }
    }

 

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐