unity请求http网络数据有两种方式。一种是POST模式,另一种是GET模式。根据需求不一样,可选择自己喜欢的模式请求数据。请求数据是有些些细节需要注意一下
//POST模式:
IEnumerator AA(string url, string postData,string temp,string timestamp)
{
using (UnityWebRequest www = new UnityWebRequest(url, “POST”))
{
byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(postData);//postData表示请求时带的数据
www.uploadHandler = (UploadHandler)new UploadHandlerRaw(postBytes);
www.SetRequestHeader(“x-dbsv1-sign”, temp);//设置表头数据1
www.SetRequestHeader(“x-dbsv1-timestamp”, timestamp);//设置表头数据2
www.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
www.SetRequestHeader(“Content-Type”, “application/json”);
yield return www.Send();
if (www.isError)
{
UnityEngine.Debug.LogError(“错误:”+www.error);
}
else
{
if (www.responseCode == 200)//请求码为200表示请求成功
{
Debug.Log(www.downloadHandler.text);//请求返回的数据
}
}
}
}