Login screen with HTTP GET request

Hi guys

I am trying to accomplish the following: have a UI where I can enter username and password. Upon pressing a button, a HTTP GET request should be made to a server to authenticate username and password. The server returns “0” or “1”. The UI should then (as a start) just display “Login successful” or “Login failed”.

I have the server running, I have the Unity UI running.
I have also tried to find a solution myself, but I keep getting hung up on
a) the old / new way of making HTTP requests
b) coroutines

How do I make the HTTP GET request from the button in my UI?

Any helpful piece of code would be extremely appreciated.

Thank you very much in advance!

It’s pretty simple, but you do need to put it in a coroutine function:

WWW req = new WWW(url);
yield return www;

if(!String.IsNullOrEmpty(www.error))
{
    String response = www.text;
    // do your logic with the response
}
else
{
    //Handle error gotten from www.error
}

Hey @Fonkler

I’m working on a service that will provide a nice Unity plugin for such a thing and a cloud service at the back end. The Unity code works and it’s the cloud service I’m working on.

I’ve found that I’m much better off with UnityEngine.Networking.UnityWebRequest than the old WWW class. It’s much more capable than the old WWW class. Handles all the HTTP methods and deals with HTTPS.

I’m sure you want to encrypt your users’ credentials.

Also, when providing parameters to an HTTP(S) server, you probably want to use a POST.

The use of coroutines is for any moment where you wait for a response or event. You don’t have_to use a coroutine but not doing so means stuttery frames.