Hi everyone and thanks for your help!
I am trying to ask my web application if a user is ok to connect or not and I am currently struggling with webrequest on android.
First I worked with HttpWebRequest and it looks like this does not work on Android (code below) :
private bool TryConnect(string email, string password) {
var cert = new ForceAcceptAll();
string siteAddress = webSiteAddress + $"/Account/LoginSmartphone?email={email}&password={password}";
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(siteAddress );
webRequest.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
webRequest.Method = "GET";
using (HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse()) {
var html = new StreamReader(response.GetResponseStream()).ReadToEnd();
bool.TryParse(html, out bool connectionOk);
return connectionOk;
}
}
It worked perfectly on computer but I had no response on Android. So after searching on Google I found this solution instead :
IEnumerator GetRequest(string uri) {
using (UnityWebRequest webRequest = UnityWebRequest.Get(uri)) {
Debug.Log("Before send request");
//webRequest.certificateHandler = new ForceAcceptAll();
// Request and wait for the desired page.
yield return webRequest.SendWebRequest();
Debug.Log("Send request done");
switch (webRequest.result) {
case UnityWebRequest.Result.ConnectionError:
case UnityWebRequest.Result.DataProcessingError:
case UnityWebRequest.Result.ProtocolError:
//Error in login
Debug.LogError("Error: " + webRequest.error);
ConnectionError();
break;
case UnityWebRequest.Result.Success:
Debug.Log("Received: " + webRequest.downloadHandler.text);
if (ConnectionResponse(webRequest.downloadHandler.text)) {
ConnectionOk();
} else {
ConnectionError();
}
break;
}
}
}
On computer it works perfectly but when running on Android Emulator I have a timeout everytime. I hit the Debug.Log("Before send") but never the Debug.Log("Send Request done").
Does anyone know why is that and how to manage it properly?
Thanks and have a nice day!
↧