Unity Http Post請求之Json參數(shù)
使用Litjson
void Start()
{
//使用litJson創(chuàng)建json格式的參數(shù)數(shù)據(jù)
JsonData data = new JsonData();
data["與后端協(xié)商好的參數(shù)名"] = "你要寫入的參數(shù)";
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);
}
}
聲明:文章內(nèi)容整理來源于網(wǎng)絡(luò),版權(quán)屬于原作者,如有問題,請聯(lián)系我們!
來源:https://www.cnblogs.com/unity3ds/p/12680239.html
- 上一篇:UNIT3D更改界面字體大小顏色 2021/5/5
- 下一篇:Qt QSerialPort 類實現(xiàn)串口通信 2021/4/23


