我有一個 JSON 結構如下:
{
"values": {
"AppName": "Test001",
"AppUser": "Testttt"
},
"consentAccepted": true,
"consentToken": "adgrtztzEZKo3LD56Hjo8LiqeoQ2Z5 ik0loplr"
}
上面的 JSON 在https://web.postman.co/ 中有效,我的狀態為成功。
我想用 POST 嘗試使用 Unity 的 WebRequest 來填寫“AppName”和“AppUser”以及令牌。我如何實作這一目標?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class SendData: MonoBehaviour {
void Update () {
if (InputCompat.GetKeyDown (KeyCode.P)) {
StartCoroutine ("FillAndSend");
}
}
public IEnumerator FillAndSend() {
WWWForm form = new WWWForm ();
form.AddField ("AppName", "Testttt");
form.AddField ("AppUser", "Reinnn");
UnityWebRequest www = UnityWebRequest.Post ("https://mywebsite.com/profile/MyApp", form);
www.SetRequestHeader ("token", "tz677zuiuis2qEZKo3Lt kHluOGss");
yield return www.SendWebRequest ();
if (www.isNetworkError || www.isHttpError) {
Debug.Log (www.error);
} else {
Debug.Log (www.downloadHandler.text);
}
}
}
我收到一個名為 HTTP 內部服務器錯誤的錯誤。它有什么問題?與此同時,我必須添加一個特定的引數,如下所示:
formToken: MyApp
uj5u.com熱心網友回復:
看起來form你更想發送一個 JSON 字串而不是一個。
就像是
var json = "{\"values\": {\"AppName\":\"Test001\",\"AppUser\":\"Rein\"}, \"consentAccepted\":true, \"consentToken\":\"tz677zuiuis2qEZKo3Lt kHluOGss\"}";
var jsonBytes = Encoding.UTF8.GetBytes(json);
using (var www = new UnityWebRequest("https://mywebsite.com/profile/MyApp", "POST"))
{
www.uploadHandler = new UploadHandlerRaw(jsonBytes);
www.downloadHandler = new DownloadHandlerBuffer();
www.SetRequestHeader("Content-Type", "application/json");
www.SetRequestHeader("Accept", " text/plain");
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Debug.Log(www.downloadHandler.text);
}
}
為了輕松地配置它,例如通過 Inspector 或傳遞值而不必手動撰寫 JSON,您可以輕松地擁有一個類,例如
[Serializable]
public class PostData
{
public Values values;
public bool consentAccepted;
public string consentToken;
}
[Serializable]
public class Values
{
public string AppName;
public string AppUser;
}
然后簡單地做例如
var jsonData = new PostData
{
values = new Values
{
AppName = "Test001",
AppUser = "Rein"
},
consentAccepted = true,
consentToken = "tz677zuiuis2qEZKo3Lt kHluOGss"
}
var json = JsonUtility.ToJson(jsonData);
var jsonBytes = Encoding.UTF8.GetBytes(json);
uj5u.com熱心網友回復:
用戶derHugo的解決方案是對的,只是代碼中的JSON結構有誤。我修復了它,這是完整的解決方案。
private string json = @"{
'values': {
'AppName': 'Test001',
'AppUser': 'Rein'
},
'consentAccepted': true,
'consentToken': 't65wRU6rttK1klzu768'
}";
var jsonBytes = Encoding.UTF8.GetBytes(json);
using (var www = new UnityWebRequest("https://mywebsite.com/profile/MyApp", "POST"))
{
www.uploadHandler = new UploadHandlerRaw(jsonBytes);
www.downloadHandler = new DownloadHandlerBuffer();
www.SetRequestHeader("Content-Type", "application/json");
www.SetRequestHeader("Accept", " text/plain");
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Debug.Log(www.downloadHandler.text);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/360394.html
上一篇:為JSON的屬性提供值
