HTTP协议使用POST方式向服务器发送Json数据——C#篇

在unity5.6.4中使自身携带的UnityWebRequest完成向服务器发送Json数据,具体如下代码

 IEnumerator sendDataToServer()
    {
       

        Debug.Log("向服务器发送请求:"+ jsonStr);
        byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonStr);
        UnityWebRequest request = new UnityWebRequest(serverAddress, "POST");

          request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
          request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
          request.SetRequestHeader("Content-Type", "application/json;charset=utf-8");

          yield return request.Send();

          if (request.isError)
          {

              Debug.Log("http 请求错误:" + request.error);

          } else        {

              string result = request.downloadHandler.text;
              responseText.text = result;
              Debug.Log("接收返回"+result);
          }
    }

 

 

你可能感兴趣的:(C#)