求大神把下面這個代碼改成 UnityWebRequest 請求
IEnumerator Get(string url, string auth, string json)
{
Dictionary<string,string> header = new Dictionary<string, string>();
byte[] postBytes;
header.Add("Content-Type", "application/json");//添加header
if(auth != null)
{
header.Add("Authorization",auth);
}
if(json != null)
{
//POST
//將資料轉換成json資料流
postBytes = System.Text.Encoding.UTF8.GetBytes(json);
}
else
{
//GET
//此案例不需要json資料所以為null
postBytes=null;
}
WWW www = new WWW (url,postBytes,header);
yield return www;
if (www.error != null)
{
Debug.Log("error is :"+ www.error);
}
else
{
Debug.Log("request result :" + www.text);
}
}
uj5u.com熱心網友回復:
UnityWebRequest request;IEnumerator Get(string url, string auth, string json)
{
using(request = UnityWebRequest.Post(url, json != null ? json : ""))
{
request.SetRequestHeader("Content-Type", "application/json");
if (auth != null)
{
request.SetRequestHeader("Authorization-Type", auth);
}
yield return request.SendWebRequest();
if (request.isHttpError || request.isNetworkError)
{
Debug.Log("error is :" + request.error);
}
else
{
Debug.Log("request result :" + request.downloadHandler.text);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/23890.html
標籤:Unity3D
