Answer the question
In order to leave comments, you need to log in
How to get the response from the server into a method?
I wrote such a simple authorizer, here is the method itself hung on the button
public class Authorization : MonoBehaviour{
private string login;
private string password;
public string answer;
public void ClickAuthorization(){
...........
Dictionary<string,string> a = new Dictionary<string,string>();
a.Add("login",login);
a.Add("password",password);
Server.Inst.SendData(a);
//answer = Server.Inst.SendData(a);
//Debug.Log(answer);
}
}
public class Server: MonoBehaviour{
public static string answer;
public static Server Inst;
private void Awake(){ Inst = this; }
public void SendData(Dictionary<string,string> a){
StartCoroutine(SendDataWWW(a));
}
IEnumerator SendDataWWW(Dictionary<string,string> a){
WWWForm form = new WWWForm();
foreach(KeyValuePair<string,string> v in a){
form.AddField(v.Key,v.Value);
}
UnityWebRequest www = UnityWebRequest.Post("https://..............", form);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError){
answer="error";
}else{
answer=www.downloadHandler.text;
}
Debug.Log("answer server => "+answer);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question