Я пытаюсь запросить данные с сервера node.js, передав ему jsondata из сценария Unity, но соединение продолжает терпеть неудачу, и я не уверен, что это проблема с моим кодом. API определенно работает с данными JSON, как я пробовал в Curl.
public IEnumerator Post(string url,string jsonData)
{
using (UnityWebRequest www = UnityWebRequest.PostWwwForm(url, jsonData))
{
www.method = UnityWebRequest.kHttpVerbPOST;
www.certificateHandler = new BypassCertificate();
byte[] dataByte = new System.Text.UTF8Encoding().GetBytes(jsonData);
www.uploadHandler = (UploadHandler)new UploadHandlerRaw(dataByte);
www.SetRequestHeader("Content-Type", "application/json");
//if (expectedReturn != null) //always have download handler to handle error data being sent over
www.downloadHandler = (DownloadHandlerBuffer)new DownloadHandlerBuffer();
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError)
{
// Handle error
Debug.LogError(www.error);
}
else
{
// Handle successful response
Debug.Log("Response: " + www.downloadHandler.text);
}
}
}
}
// Class to bypass certificate validation
public class BypassCertificate : CertificateHandler
{
protected override bool ValidateCertificate(byte[] certificateData)
{
// Always return true to bypass certificate validation
return true;
}
}
Подробнее здесь: https://stackoverflow.com/questions/784 ... from-unity