UnityWebRequest 发送Json数据给后端

在很多时候我们需要把json数据传递给后端使用,这里提供两种方案。

方案一:

private void Start()
{
    string url="xxx";
    string json="一个Json格式的数据,这里大家替换成自己想要测试的Json数据";
    StartCoroutine(I_RequestByJsonBodyPost(url,json));
}

private static IEnumerator I_RequestByJsonBodyPost(string url, string json)
{    
     UnityWebRequest www = new UnityWebRequest(url, UnityWebRequest.kHttpVerbPOST);
     DownloadHandler downloadHandler = new DownloadHandlerBuffer();
     www.downloadHandler = downloadHandler;                                    
     www.SetRequestHeader("Content-Type", "application/json;charset=utf-8");       
     byte[] bodyRaw = Encoding.UTF8.GetBytes(json);
     www.uploadHandler = new UploadHandlerRaw(bodyRaw);
     yield return www.SendWebRequest();
     Debug.Log(www.downloadHandler.text);
 }

为了保证前端发送的是 Json 格式的数据,让后端能够正常地解析,必须要把请求头中的 Content-Type 设为 “application/json”

方案二:

private void Start()
{
    string url="xxx";
    string json="一个Json格式的数据,这里大家替换成自己想要测试的Json数据";
    StartCoroutine(I_RequestByJsonBodyPost(url,json));
}

private static IEnumerator I_RequestByJsonBodyPost(string url, string json)
{
     UnityWebRequest www = UnityWebRequest.Post(url, json);                                
     www.SetRequestHeader("Content-Type", "application/json;charset=utf-8");       
     byte[] bodyRaw = Encoding.UTF8.GetBytes(json);
     www.uploadHandler = new UploadHandlerRaw(bodyRaw);
     yield return www.SendWebRequest();
     Debug.Log(www.downloadHandler.text);
 }

因为 UnityWebRequest.Post 默认把 Content-Type 当作 application/x-www-form-urlencoded,所以我们传进去的字符串会被 URLEncoded,因此只能手动创建一个 uploadHandler,覆盖掉原来的 uploadHandler。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity中的UnityWebRequest是一个用于处理网络请求的类,可以通过POST方法发送JSON数据。通过UnityWebRequest类,可以轻松地向服务器发送一个包含JSON数据的POST请求。要使用UnityWebRequest发送POST请求,首先需要创建一个包含JSON数据的字符串,然后将其转换为字节数组,并设置请求的Content-Type为"application/json"。接着,构建一个UnityWebRequest对象,设置其URL和请求方式为POST,并将JSON数据添加到请求中。最后,发送请求并等待服务器的响应。 以下是一个简单的示例代码: ```csharp string url = "http://example.com/api"; string jsonData = "{\"key1\": \"value1\", \"key2\": \"value2\"}"; byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonData); UnityWebRequest request = new UnityWebRequest(url, "POST"); request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw); request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer(); request.SetRequestHeader("Content-Type", "application/json"); yield return request.SendWebRequest(); if (request.result != UnityWebRequest.Result.Success) { Debug.LogError(request.error); } else { Debug.Log("Post successful!"); } ``` 上述代码中,我们首先创建了一个包含JSON数据的字符串jsonData,然后将其转换为字节数组bodyRaw。接着,我们创建了一个UnityWebRequest对象request,设置了请求的URL和方法,并将JSON数据添加到请求中。最后,通过SendWebRequest方法发送POST请求,并根据响应的结果做出相应的处理。这样,就可以使用UnityWebRequest发送包含JSON数据的POST请求了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值