Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

[SOLVED] WebGL Build problem with WebRequest RequestStream

Discussion in 'Scripting' started by whileBreak, Jan 4, 2018.

  1. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    We are having a problem with a web request. This script is working just fine when build for Android, but when compiling it for WebGL its freezing on the browser. Applying some debugging to console we found that the problem is in the line marked in between # but we can't seem to find an answer of what might be happening.
    ATM we will try to use this way now https://docs.unity3d.com/Manual/UnityWebRequest-SendingForm.html and hope that fix it.
    Code (CSharp):
    1.  
    2. LoginInfo newLogin = new LoginInfo(_usuario);
    3.         bool succesful = false;
    4.  
    5.         if (currentLogin.user != newLogin.user || (currentLogin.user == newLogin.user && !currentLogin.isLoggedIn))
    6.         {
    7.             Debug.Log("Set Request");
    8.             httpVerb httpMethod = httpVerb.POST;
    9.  
    10.             string strResponseValue = string.Empty;
    11.  
    12.             string postData = "email=" + usuario + "&password=" + password;
    13.             //string postData = "";
    14.             ASCIIEncoding encoding = new ASCIIEncoding();
    15.             byte[] byte1 = encoding.GetBytes(postData);
    16.  
    17.             //Set the content type of the data being posted.
    18.  
    19.  
    20.             //Set the content length of the string being posted.
    21.             Debug.Log("Accept all certifications");
    22.             ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
    23.  
    24.             Debug.Log("Create WebRequest");
    25.             HttpWebRequest request = (HttpWebRequest)WebRequest.Create("URL URL URL :P");
    26.  
    27.             request.Method = httpMethod.ToString();
    28. request.ContentType = "application/x-www-form-urlencoded";
    29.             request.ContentLength = byte1.Length;
    30.  
    31. //########The problem is in this line###########
    32.             Stream newStream = request.GetRequestStream();
    33. //########################################
    34.             newStream.Write(byte1, 0, byte1.Length);
    35.  
    36.             newStream.Close();
    37.  
    38. //After this we just get the response
    39.  
     
  2. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
  3. dradb

    dradb

    Joined:
    Jan 10, 2015
    Posts:
    86
    I'm using exactly that but I can't get it to work in WebGL. Works fine in the editor though.
    I can't get any returned data in WebGL.
    Any hints please?
     
  4. Dr_Shtenzel

    Dr_Shtenzel

    Joined:
    Jan 5, 2017
    Posts:
    2
    It looks like you are using System.Net.HttpWebRequest, that non-functional on WebGL platforom according to this page https://docs.unity3d.com/Manual/webgl-networking.html

    So you might end up using UnityWebRequest instead of HttpWebRequest
     
    Champilas likes this.
  5. Harpaceas

    Harpaceas

    Joined:
    Feb 15, 2021
    Posts:
    4
    for webGL build You can also make the request by javascript and then call the unity public function you need:

    "gameInstance.SendMessage(<YourGameObject>, <ThePublicFunctionYouWantToCallInAScriptInYourGameObject>, <parameters>)