Search Unity

Microsoft Edge, UnityWebRequest not working

Discussion in 'Web' started by threedinteractive, Feb 15, 2019.

  1. threedinteractive

    threedinteractive

    Joined:
    Jul 18, 2018
    Posts:
    8
    I am having a problem with UnityWebRequests in the Edge Browser (Version 44.17763.1.0).
    When calling the following code I get no connection to my server established when using edge browser.

    Code (CSharp):
    1. Uri uri = new Uri("http://localhost:1337/xyz");
    2.  
    3. using (UnityWebRequest www = new UnityWebRequest(uri, UnityWebRequest.kHttpVerbPOST))
    4. {
    5.     JSONRequest data = getRequestedData();
    6.     var byteData= Encoding.ASCII.GetBytes( JsonUtility.ToJson(data) );
    7.  
    8.     www.timeout = 10;
    9.     www.downloadHandler = new DownloadHandlerBuffer();
    10.     www.uploadHandler = new UploadHandlerRaw(byteData);
    11.     www.SetRequestHeader("Content-Type", "text/json");
    12.  
    13.     yield return www.SendWebRequest();
    14.  
    15.     if(www.isNetworkError || www.isHTTPError)
    16.         throw new Exception("Error");  
    17. }
    18.  
    There are no warnings or errors in the browser or debug console, just a TimeOut Exception is thrown.
    In Firefox, the same code works fine.

    Some additional Information:
    Appears in both ASM.js and Webassembly,

    I hope it can be helped because edge compatibility is important to us.
    Greetings
     
    Last edited: Feb 15, 2019
  2. threedinteractive

    threedinteractive

    Joined:
    Jul 18, 2018
    Posts:
    8
    I wrote a little example script to test the other HTTP verbs.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Text;
    4. using TMPro;
    5. using UnityEngine;
    6. using UnityEngine.Networking;
    7. using UnityEngine.UI;
    8.  
    9. public class ConnectionTest : MonoBehaviour
    10. {
    11.     Uri m_Uri = new Uri("http://localhost:1337/debug");
    12.  
    13.     public TMP_InputField m_Input;
    14.  
    15.     public Image m_Get;
    16.     public Image m_Post;
    17.     public Image m_Put;
    18.     public Image m_Delete;
    19.  
    20.     private Color m_Color = new Color();
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         m_Color = m_Get.color;
    26.         m_Input.text = m_Uri.ToString();
    27.         StartCoroutine(Load());      
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {      
    33.     }
    34.  
    35.     IEnumerator Load()
    36.     {
    37.         yield return StartCoroutine(LoadRoutine(m_Uri, UnityWebRequest.kHttpVerbGET));
    38.         yield return StartCoroutine(LoadRoutine(m_Uri, UnityWebRequest.kHttpVerbPOST));
    39.         yield return StartCoroutine(LoadRoutine(m_Uri, UnityWebRequest.kHttpVerbPUT));
    40.         yield return StartCoroutine(LoadRoutine(m_Uri, UnityWebRequest.kHttpVerbDELETE));
    41.     }
    42.  
    43.     public void setUri(string uri)
    44.     {
    45.         if(!string.IsNullOrEmpty(uri) && !string.IsNullOrWhiteSpace(uri))
    46.         {
    47.             m_Get.color = m_Color;
    48.             m_Post.color = m_Color;
    49.             m_Put.color = m_Color;
    50.             m_Delete.color = m_Color;
    51.  
    52.             m_Uri = new Uri(uri);
    53.  
    54.             StartCoroutine(Load());
    55.         }
    56.     }
    57.  
    58.     IEnumerator LoadRoutine(Uri uri, string requestType)
    59.     {
    60.         Debug.Log("Testing " + requestType + " to " + uri.ToString());
    61.         using (UnityWebRequest www = new UnityWebRequest(uri, requestType))
    62.         {
    63.             //www.chunkedTransfer = true;
    64.             //www.timeout = options.timeout;
    65.             www.downloadHandler = new DownloadHandlerBuffer();
    66.             if(requestType != UnityWebRequest.kHttpVerbGET)
    67.             {
    68.                 www.uploadHandler = new UploadHandlerRaw(Encoding.ASCII.GetBytes("test1234"));
    69.                 www.SetRequestHeader("Content-type", "text/json");
    70.             }
    71.             yield return www.SendWebRequest();
    72.  
    73.             if (www.isHttpError)
    74.                 Debug.LogError("HTTPError: " + www.error);
    75.  
    76.             if (www.isNetworkError)
    77.                 Debug.LogError("NetworkError: " + www.error);
    78.  
    79.             bool hasError = www.isHttpError || www.isNetworkError;
    80.             switch (requestType)
    81.             {
    82.                 case UnityWebRequest.kHttpVerbGET   : m_Get.color    = hasError ? Color.red : Color.green; break;
    83.                 case UnityWebRequest.kHttpVerbPOST  : m_Post.color   = hasError ? Color.red : Color.green; break;
    84.                 case UnityWebRequest.kHttpVerbPUT   : m_Put.color    = hasError ? Color.red : Color.green; break;
    85.                 case UnityWebRequest.kHttpVerbDELETE: m_Delete.color = hasError ? Color.red : Color.green; break;
    86.             }
    87.  
    88.             Debug.Log(www.downloadHandler.text);
    89.         }
    90.     }
    91. }
    Here is the copied edge console window.

    Like I said, in Firefox everything is working fine.
     
  3. kahyong_unity

    kahyong_unity

    Unity Technologies

    Joined:
    Jan 17, 2019
    Posts:
    5
    Hi,

    Just like to update this thread so that I can share what happened in the ticket that we have concluded:

    If you open your project in the Chrome browser, you will get a more detailed error: CORS policy: Cross-origin requests are only supported for protocol schemes: HTTP, data, chrome, chrome-extension, https.
    Edge doesn't show this extra detail in its error.

    Further digging in StackOverflow (I.e.https://stackoverflow.com/questions...-for-http-error-when-loading-a-local/10752078) shows that there are cross-domain security restrictions on modern browsers and the way to move forward with this is hosting the application via a web server, which you already verified that it works for you.

    Thanks.