UnityWebRequest class analysis

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:

  1. The UnityWebRequest object is used to communicate with the web server.
  2. The unitywebre task handles the HTTP communication flow with the web server. Other objects—notably DownloadHandler and UploadHandler—manage download and upload data, respectively.
  3. 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.
  4. 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:

kHttpVerbCREATE

The string "CREATE", usually used as the verb of the HTTP CREATE request.

kHttpVerbDELETE

The string "DELETE", usually used as the verb for HTTP DELETErequest.

kHttpVerbGET

The string "GET", typically used as the verb for HTTP GETrequest.

kHttpVerbHEAD

The string "HEAD", typically used as the verb for HTTP HEADrequest.

kHttpVerbPOST

The string "POST", usually used as the verb for HTTP POSTrequest.

kHttpVerbPUT

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

certificateHandler

Holds a reference to the certificate handler object responsible for managing certificate validation for UnityWebRequest.

chunkedTransfer

Indicates whether the UnityWebRequest system should use the HTTP/1.1 transfer encoding method.

disposeCertificateHandlerOnDispose

If true, then any certificate handler connected to the UnityWebRequest will have a certificate handler. Automatically invoked when UnityWebRequest is processed.

disposeDownloadHandlerOnDispose

If true, then any download handler connected to this UnityWebRequest will have a DownloadHandler. Called automatically when UnityWebRequest. deal with.

disposeUploadHandlerOnDispose

If true, any UploadHandler attached to this UnityWebRequest will have the UploadHandler. Automatically invoked when UnityWebRequest is processed.

downloadedBytes

Returns the number of bytes of body data that the system downloaded from the remote server. (read only)

downloadHandler

Holds a reference to a DownloadHandler object that manages the body data received by this UnityWebRequest from the remote server.

downloadProgress

Returns a floating point value between 0.0 and 1.0 indicating the progress of downloading data from the server. (read only)

error

A human-readable string describing any system errors that the UnityWebRequest object encountered while processing an HTTP request or response. (read only)

isDone

Returns true after UnityWebRequest has finished communicating with the remote server. (read only)

isHttpError

Returns true after this UnityWebRequest receives an HTTP response code indicating an error. (read only)

isModifiable

Returns true when the configuration property of UnityWebRequest can be changed. (read only)

isNetworkError

Returns true after this UnityWebRequest encounters a system error. (read only)

method

Define the HTTP verb (that is, the above static property) used by this UnityWebRequest, such as GET or POST.

redirectLimit

Indicates the number of redirects that UnityWebRequest will follow before stopping with the "Redirect Limit Exceeded" system error.

responseCode

The numeric HTTP response code returned by the server, such as 200, 404 or 500. (read only)

timeout

Sets UnityWebRequest to attempt to abort after a timeout in seconds.

uploadedBytes

Returns the number of bytes of body data uploaded by the system to the remote server. (read only)

uploadHandler

Holds a reference to an UploadHandler object that manages the body data to be uploaded to the remote server.

uploadProgress

Returns a floating point value between 0.0 and 1.0 indicating the progress of uploading data to the server.

uri

Defines the target URI that UnityWebRequest communicates with.

url

Defines the target URL that UnityWebRequest communicates with.

useHttpContinue

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

Abort

If there is progress, stop the UnityWebRequest as soon as possible.

Dispose

This is a signal that this UnityWebRequest is no longer in use and any resources it uses should be cleaned up.

GetRequestHeader

Retrieves the value of a custom request header.

GetResponseHeader

Retrieves the value of the response header from the latest HTTP response received.

GetResponseHeaders

Retrieve a dictionary containing all response headers received by this UnityWebRequest in the latest HTTP response.

SendWebRequest

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.

SetRequestHeader

Set an HTTP request header to a custom value.

4. Public methods

Abort

If there is progress, stop the UnityWebRequest as soon as possible.

Dispose

This is a signal that this UnityWebRequest is no longer in use and any resources it uses should be cleaned up.

GetRequestHeader

Retrieves the value of a custom request header.

GetResponseHeader

Retrieves the value of the response header from the latest HTTP response received.

GetResponseHeaders

Retrieve a dictionary containing all response headers received by this UnityWebRequest in the latest HTTP response.

SendWebRequest

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.

SetRequestHeader

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);
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_40833062/article/details/129278440