Unity 发送 Json 数据 PUT GET POST 的三种方式

PUT

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://120.78.72.125:28088/usercenter/users-anon/password?newPassword=" + newPass + "&verifiedCode="+ passCode);
        request.ContentType = "text/html:charset=UTF-8";
        request.Method = "PUT";
        request.UserAgent = null;
        request.Timeout = 5000;
        //using (var streamWriter = new StreamWriter(request.GetRequestStream()))
        //{
        //    string json = "{" +
        //        "'newPassword': '"+ newPass + "'," +
        //        "'verifiedCode': '" + passCode + "'," +  
        //        "}";
        //    streamWriter.Write(json);
        //}
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream myResponseStream = response.GetResponseStream();
        StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
        string retString = myStreamReader.ReadToEnd();
        myStreamReader.Close();
        myResponseStream.Close();
        Debug.Log(retString);
        MsgData reData = JsonMapper.ToObject<MsgData>(retString);
        errorTextTip.text = reData.responseDescription;
        StartCoroutine(CloseErrorTip());
        if (reData.statusCode == 200)
        {
            gameObject.SetActive(false);
        }
        else
        {
            errorTextTip.text = "验证失败";
            StartCoroutine(CloseErrorTip());
        }

GET

 string strPath = "http://120.78.72.125:28088/usercenter/users-anon/forget-password?userName=" + phoneNumber + "&userType=PHONE";

        #region
        UnityWebRequest request = new UnityWebRequest(strPath);
        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.Log("链接服务器异常:" + request.error);
        }
        else
        {
            //执行倒计时
            Debug.Log(request.downloadHandler.text + "PPPPPPPPPPPPPPPPPPPPPPPPPPPP");
            CodeData root = JsonMapper.ToObject<CodeData>(request.downloadHandler.text);
            Debug.Log(root.statusCode + "PPPPPPPPPPPPPPPPPPPPPPPPPPPP");

            if (root.statusCode == 200)
            {
                errorTextTip.text = "发送验证码成功";
                StartCoroutine(CloseErrorTip());
                isOpenTimer = true;
                btn_Send.enabled = false;
            }
            else
            {
                errorTextTip.text = "发送验证码失败";
                StartCoroutine(CloseErrorTip());

            }

POST

        Dictionary<string, string> headers = new Dictionary<string, string>();
        headers["Content-Type"] = "application/json";
        string strPath = "http://120.78.72.125:28088/usercenter/users-anon/register";
        string randomCode = passCode.text;
        int terminalKindId = 1;
        string userPwd = password.text;
        string userType = "PHONE";
        string username = phone.text;
        Debug.Log("<color=red>"+randomCode+"</color>");
        Debug.Log("<color=green>" + terminalKindId + "</color>");
        Debug.Log("<color=blue>" + userPwd + "</color>");
        Debug.Log("<color=red>" + userType + "</color>");
        Debug.Log("<color=green>" + username + "</color>");

        string strJson = "{\"randomCode\":\""+ randomCode + "\",\"terminalKindId\":\"" + terminalKindId + "\",\"userPwd\":\""+ userPwd +"\",\"userType\":\""+userType+ "\",\"username\":\"" + username + "\"}";
        WWW www = new WWW(strPath, System.Text.Encoding.Default.GetBytes(strJson), headers);
        yield return www;
        if (www.isDone)
        {
            //RegisterData
            //{"statusCode":410,"responseDescription":"注册用户不成功,请稍后再试","innerException":""}
            Debug.LogError("收到服务器回复++++++++++++++++++++++");
            print(www.text);
            RegisterData  reData = JsonMapper.ToObject<RegisterData>(www.text);  
            if (reData.statusCode == 200)
            {
                errorTextTip.text = "注册成功";
                StartCoroutine(CloseErrorTip());
                Debug.Log("注册成功跳转登录界面");
                gameObject.SetActive(false);
            }
            else
            {
                //显示一个提示框
                Debug.Log("错误描述"+reData.statusCode);
                errorTextTip.text = "注册失败";
                StartCoroutine(CloseErrorTip());


            }

Json解析格式

  public class RegisterData
    {
            /// <summary>
            public int statusCode { get; set; }
            /// <summary>
            public string responseDescription { get; set; }
            /// <summary>
            public string innerException { get; set; }
    }
    public class CodeData
    {
        public int statusCode { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string responseDescription { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string innerException { get; set; }
        public string check { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string mobile { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string registered { get; set; }

    }
  public class MsgData
    {
        /// <summary>
        public int statusCode { get; set; }
        /// <summary>
        public string responseDescription { get; set; }
        /// <summary>
        public string innerException { get; set; }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值