Web GL with Net

I have a .Net based API I make calls to regularly in my code. I recently discovered Web GL on Unity as a good option for going web based. However, I have found its not working with my API currently.

Here is a snippet of how i currently make requests:

string uri = WBG_Config._EndPoint + "api/WhiteBoXGaming/Post_UserLogIn";

        //HTTP Request
        StartCoroutine(booleanwebrequest(uri, WBG_Config._Log_In));

Here i’m just performing a simple request to log in:

IEnumerator booleanwebrequest(string uri, object obj)
    {
        try
        {
            string json = JsonConvert.SerializeObject(obj);
            var client = new HttpClient();
            var webRequest = WebRequest.Create(uri);

            webRequest.Method = "POST";
            webRequest.ContentType = "application/json";

            using (var streamWriter = new StreamWriter(webRequest.GetRequestStream()))
            {
                streamWriter.Write(json);
            }

            var httpResponse = (HttpWebResponse)webRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                _Response = Convert.ToBoolean(result);
            }
        }
        catch (Exception ex)
        {
            Debug.Log(ex);
            _Response = false;
        }

        yield return _Response;
    }

Log In Object In Unity

 public static Log_In _Log_In = new Log_In();

Object in my Dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WBG_4
{
    [System.Serializable]
    public class Log_In
    {
        public string Email;
        public string Password;
    }
}

Welcome to suggestions

One thought I had was do I need to make the classes inside Unity instead of a .Net dll and create the object properties via script?

Researched a little more IL2CPP appears to be my issue currently. So how do I convert my class to be compliment or work rather with IL2CPP? I’m currently building everything in Mono/.Net Dll. All of this seems to be going out the window for Windows Universal Platform which no one uses but I digress. So how do we switch to support IL2CPP if we were .Net prior?

The web application framework you are using for your API shouldn’t make any difference whatsoever to how you interact with it. Whether it’s .NET, Java Spring, Django, etc… doesn’t matter. You send it HTTP requests, and it responds in kind.

What you should do is elaborate on what you mean by “its not working”. Are you receiving errors when making requests to the API?

Any reason why you don’t use UnityWebRequests? It seems the request specific code is still part of your Unity project, so that’d make the most sense.

Also, there’s no need for all of that to be a coroutine if you call the GetResponse method (which is the synchronous version) rather than GetResponseAsync.
At the end of the coroutine, you seem to yield a boolean, which is probably not what you actually want. It’ll just be boxed, then yielded and the coroutine effectively waits only a single frame for no apparent reason after the response is already received… if you want to wait for something, it’d be an async request.

Back to Unity’s web request, you can skip all the http client stuff and streams. You simply create a web request, yield the async operation returned when you send it. When it’s done, check for errors & handle it or continue processing the result.

Straightforward example can be found here. Note that the example does only check for a network error, but there’s also a property for http errors.

1 Like

AFAIK at least in the past you couldn’t use any of the .NET networking classes as they are all based on socket connections. In a WebGL build you can not use any direct / custom socket connections. Your application runs in the JS sandbox of the browser. The only networking capabilities you have at hand are the browsers own http APIs (i.e. XMLHttpRequest). Unity’s UnityWebRequest has a different implementation for each targetplatform. This isn’t (wasn’t ?!) the case for the standard .NET networking stuff.

While the UnityWebRequest is significantly better than the WWW class, it’s still not a 1-to-1 replacement for the .NET WebRequest / HttpClient. So yes, you probably have to manually replace those .NET classes with an equivalent Unity compatible implementation. Depending on the functionality needed this could be impossible or take quite a bit of time.

Thanks Bunny, I created this class adjustment but i’m not sure if this is correct. I assume I still need to switch to UnityWebRequest as well or will coroutine still work?

using UnityEngine;

[System.Serializable]
public class Unity_Log_In
{
    public string Email;
    public string Password;
}