Home > other >  Unity, come in please, solve the problem of simple
Unity, come in please, solve the problem of simple

Time:09-28

Pray god, with the following code into UnityWebRequest request


IEnumerator Get (url string, the string auth, string json)
{
Dictionary The header=new Dictionary (a);
Byte [] postBytes;

The header. The Add (" the content-type ", "application/json");//add the header
If (auth!=null)
{
The header. The Add (" Authorization ", auth);
}

If (json!=null)
{
//POST
//to convert data into a json data stream
PostBytes=System. Text. Encoding. UTF8. GetBytes (json);
}
The else
{
//GET
//this case does not need the json data so null
PostBytes=null;
}

WWW WWW=new WWW (url, postBytes, header);
Yield return WWW.

If (www.error!=null)
{
The Debug Log (" the error is: "+ www.error);
}
The else
{
The Debug Log (" request result: "+ www.text);
}

}

CodePudding user response:

UnityWebRequest request;
IEnumerator Get (url string, the string auth, string json)
{
Using (request=UnityWebRequest. Post (url, json!=null? Json: ""))
{
Request. SetRequestHeader (" the content-type ", "application/json");
If (auth!=null)
{
Authorization request. SetRequestHeader (" -type "auth);
}
Yield return request. SendWebRequest ();

If (request. IsHttpError | | request. IsNetworkError)
{
The Debug Log (" the error is: "+ request. The error).
}
The else
{
The Debug Log (" request result: "+ request. DownloadHandler. The text).
}
}
}