First of all, you need to know what this class does. It mainly loads the AB package from the server or locally. The official explanation is:
- The UnityWebRequest object is used to communicate with the web server.
- The unitywebre task handles the HTTP communication flow with the web server. Other objects—notably DownloadHandler and UploadHandler—manage download and upload data, respectively.
- For convenience, a set of static functions are provided; these return UnityWebRequest objects configured correctly for many common use cases. See: Get, Post, Put, GetTexture.
- Note: When UnityWebRequest starts communicating with a remote server by calling the Send method, most properties on the UnityWebRequest object cannot be changed.
1. Static variables:
The string "CREATE", usually used as the verb of the HTTP CREATE request. |
|
The string "DELETE", usually used as the verb for HTTP DELETErequest. |
|
The string "GET", typically used as the verb for HTTP GETrequest. |
|
The string "HEAD", typically used as the verb for HTTP HEADrequest. |
|
The string "POST", usually used as the verb for HTTP POSTrequest. |
|
The string "PUT", typically used as the verb for HTTP PUTrequest. |
I don't understand what it is used for, but I haven't used it here, if I use it, I will add it
2. Properties
Holds a reference to the certificate handler object responsible for managing certificate validation for UnityWebRequest. |
|
Indicates whether the UnityWebRequest system should use the HTTP/1.1 transfer encoding method. |
|
If true, then any certificate handler connected to the UnityWebRequest will have a certificate handler. Automatically invoked when UnityWebRequest is processed. |
|
If true, then any download handler connected to this UnityWebRequest will have a DownloadHandler. Called automatically when UnityWebRequest. deal with. |
|
If true, any UploadHandler attached to this UnityWebRequest will have the UploadHandler. Automatically invoked when UnityWebRequest is processed. |
|
Returns the number of bytes of body data that the system downloaded from the remote server. (read only) |
|
Holds a reference to a DownloadHandler object that manages the body data received by this UnityWebRequest from the remote server. |
|
Returns a floating point value between 0.0 and 1.0 indicating the progress of downloading data from the server. (read only) |
|
A human-readable string describing any system errors that the UnityWebRequest object encountered while processing an HTTP request or response. (read only) |
|
Returns true after UnityWebRequest has finished communicating with the remote server. (read only) |
|
Returns true after this UnityWebRequest receives an HTTP response code indicating an error. (read only) |
|
Returns true when the configuration property of UnityWebRequest can be changed. (read only) |
|
Returns true after this UnityWebRequest encounters a system error. (read only) |
|
Define the HTTP verb (that is, the above static property) used by this UnityWebRequest, such as GET or POST. |
|
Indicates the number of redirects that UnityWebRequest will follow before stopping with the "Redirect Limit Exceeded" system error. |
|
The numeric HTTP response code returned by the server, such as 200, 404 or 500. (read only) |
|
Sets UnityWebRequest to attempt to abort after a timeout in seconds. |
|
Returns the number of bytes of body data uploaded by the system to the remote server. (read only) |
|
Holds a reference to an UploadHandler object that manages the body data to be uploaded to the remote server. |
|
Returns a floating point value between 0.0 and 1.0 indicating the progress of uploading data to the server. |
|
Defines the target URI that UnityWebRequest communicates with. |
|
Defines the target URL that UnityWebRequest communicates with. |
|
Determines whether this UnityWebRequest includes Expect: 100-Continue in its outgoing request headers. (default: true). |
The main commonly used attributes are:
downloadProgress/isDone/timeout/uri/uploadProgress
3. Static method
If there is progress, stop the UnityWebRequest as soon as possible. |
|
This is a signal that this UnityWebRequest is no longer in use and any resources it uses should be cleaned up. |
|
Retrieves the value of a custom request header. |
|
Retrieves the value of the response header from the latest HTTP response received. |
|
Retrieve a dictionary containing all response headers received by this UnityWebRequest in the latest HTTP response. |
|
Start communicating with the remote server. After calling this method, UnityWebRequest will perform DNS resolution (if necessary), send an HTTP request to the remote server at the target URL and process the server's response. This method should only be called once on any given UnityWebRequest object. Once this method is called, you cannot change any properties of UnityWebRequest. This method returns a WebRequestAsyncOperation object. Spawning a WebRequestAsyncOperation within a coroutine will cause the coroutine to pause until UnityWebRequest encounters a system error or completes communication. |
|
Set an HTTP request header to a custom value. |
4. Public methods
If there is progress, stop the UnityWebRequest as soon as possible. |
|
This is a signal that this UnityWebRequest is no longer in use and any resources it uses should be cleaned up. |
|
Retrieves the value of a custom request header. |
|
Retrieves the value of the response header from the latest HTTP response received. |
|
Retrieve a dictionary containing all response headers received by this UnityWebRequest in the latest HTTP response. |
|
Start communicating with the remote server. After calling this method, UnityWebRequest will perform DNS resolution (if necessary), send an HTTP request to the remote server at the target URL and process the server's response. This method should only be called once on any given UnityWebRequest object. Once this method is called, you cannot change any properties of UnityWebRequest. This method returns a WebRequestAsyncOperation object. Spawning a WebRequestAsyncOperation within a coroutine will cause the coroutine to pause until UnityWebRequest encounters a system error or completes communication. |
|
Set an HTTP request header to a custom value. |
Get method:
private IEnumerator SendUrl(string url)
{
using (UnityWebRequest www = UnityWebRequest.Get(url))
{
yield return www.Send();
if (www.error != null)
{
Debug.Log(www.error);
}
else
{
if (www.responseCode == 200)//200表示接受成功
{
Debug.Log(www.downloadHandler.text);
}
}
}
}
Post method:
public IEnumerator PostUrl(string url, string postData)
{
using (UnityWebRequest www = new UnityWebRequest(url,"POST"))
{
byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(postData);
www.uploadHandler = (UploadHandler)new UploadHandlerRaw(postBytes);
www.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
www.SetRequestHeader("Content-Type", "application/json");
yield return www.Send();
if (www.isError)
{
Debug.Log(www.error);
}
else
{
// Show results as text
if (www.responseCode == 200)
{
Debug.Log(www.downloadHandler.text);
}
}
}
}