- Home /
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!
So I have resolved this as follows. Not pretty, but at least functional. Out of my UI code, I start a coroutine which does the check on the backend. Inside the same coroutine, I get references to my UI objects (GameObject.find(xx)) and then modify the UI this way. Thanks for the inputs below, anyways.
Answer by FortisVenaliter · Jul 28, 2017 at 08:55 PM
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
}
Thanks for your reply. However, the part with the coroutine is one thing that I struggle with. When I put this into a coroutine, I am not in the context / class / method of the button event handler anymore. So how would I, based on the result of the HTTP GET then update part of the UI?
Answer by WazzaM · Jul 29, 2017 at 09:03 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612131419im_/https://answers.unity.com/themes/thub/images/avi.jpg)