Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Networking documentation page throwing errors

Discussion in 'Documentation' started by walsham98, Jun 8, 2022.

  1. walsham98

    walsham98

    Joined:
    Jun 19, 2021
    Posts:
    6
    I am having a look at accessing HTTP links from within my game, but when I tried working through the documentation I was having errors thrown. Specifically with the webrequest.result and UnityWebRequest.Result. Apparently these extensions are not found in UnityWebRequest?


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. // UnityWebRequest.Get example
    6.  
    7. // Access a website and use UnityWebRequest.Get to download a page.
    8. // Also try to download a non-existing page. Display the error.
    9.  
    10. public class Example : MonoBehaviour
    11. {
    12.     void Start()
    13.     {
    14.         // A correct website page.
    15.         StartCoroutine(GetRequest("https://www.example.com"));
    16.  
    17.         // A non-existing page.
    18.         StartCoroutine(GetRequest("https://error.html"));
    19.     }
    20.  
    21.     IEnumerator GetRequest(string uri)
    22.     {
    23.         using (UnityWebRequest webRequest = UnityWebRequest.Get(uri))
    24.         {
    25.             // Request and wait for the desired page.
    26.             yield return webRequest.SendWebRequest();
    27.  
    28.             string[] pages = uri.Split('/');
    29.             int page = pages.Length - 1;
    30.  
    31.             switch (webRequest.result)
    32.             {
    33.                 case UnityWebRequest.Result.ConnectionError:
    34.                 case UnityWebRequest.Result.DataProcessingError:
    35.                     Debug.LogError(pages[page] + ": Error: " + webRequest.error);
    36.                     break;
    37.                 case UnityWebRequest.Result.ProtocolError:
    38.                     Debug.LogError(pages[page] + ": HTTP Error: " + webRequest.error);
    39.                     break;
    40.                 case UnityWebRequest.Result.Success:
    41.                     Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
    42.                     break;
    43.             }
    44.         }
    45.     }
    46. }