using System; using System.Collections; using UnityEngine; using UnityEngine.Networking; public class LoginHttps : MonoBehaviour { public void Login(string userName, string password, string url, Action onSuccess, Action onFailed) { // TODO: do somethings to validate user name & password from client //... StartCoroutine(IELogin(userName, password, url, onSuccess, onFailed)); } IEnumerator IELogin(string userName, string password, string url, Action onSuccess, Action onFailed) { WWWForm form = new WWWForm(); form.AddField("uid", userName); form.AddField("pass", password); using (UnityWebRequest www = UnityWebRequest.Post(url, form)) { yield return www.SendWebRequest(); if (www.isDone) { string response = www.downloadHandler.text; // response is a json string like: {code:0,err:"wrong password"} if (!string.IsNullOrEmpty(response)) { ResponseData result = JsonUtility.FromJson(response); if (result != null) { if (result.code == 1) { Debug.Log("Login Successfully."); onSuccess?.Invoke(); } else { Debug.LogError($"Login Failure: {result.err}"); onFailed?.Invoke(); } } } else { Debug.LogError("Something wrong from response, check your backend!"); onFailed?.Invoke(); } } else { Debug.LogError(www.error); // Network error or something wrong which can not post to url. onFailed?.Invoke(); } } } [System.Serializable] class ResponseData { public int code; // 0: Login Failed, 1: Login Success public string err; // Such as: Username is not exist, Wrong password... } }