I have some deprecated errors with WWW someone knows how to convert this lines and use UnityWebRequest instead of WWW
/*this is obsolete use UnityWebRequest*/
WWW www = new WWW("http://localhost:8888/action_login.php", form);
yield return www;
if (string.IsNullOrEmpty(www.error))
{
/*this is obsolete use UnityWebRequest*/
if (www.text.Contains("invalid email or password"))
{
infoText.text = "Invalid email or password";
}
else
{
infoText.text = "Loggin Sucessful...";
}
}
else
{
infoText.text = "Error Ocurred...";
}
Answer by nratcliff · Jul 12, 2021 at 06:18 PM
It looks like you're making a POST request in the provided example. You can use UnityWebRequest.Post
for this. There's a great example on the documentation for the method: https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.Post.html
If your web service implements HTTP status codes properly you can check the value of the result
property. This is the example from the docs:
if (www.result != UnityWebRequest.Result.Success)
{
Debug.Log(www.error);
}
else
{
Debug.Log("Form upload complete!");
}
I recommend you go with that approach because it's a bit more flexible/universal. If your service responds with a different message and you're checking the message exactly, then your login code could break. If you check the status code, then the actual message doesn't matter. You could even forward www.error
right to the user so you don't have to worry about what the error message is.
If you really need to check the content of the response, you can use the attached downloadHandler
to get the text: string message = www.downloadHandler.text
See the downloadHandler documentation for more info: https://docs.unity3d.com/ScriptReference/Networking.DownloadHandler.html
Answer by francoenriquee5 · Jul 12, 2021 at 09:43 PM
Thanks a lot, Let me try that
I solved with this code, thanks a lot
public class ServerManager : MonoBehaviour
{
public TMP_InputField inputEmail;
public TMP_InputField inputPassword;
public TextMeshProUGUI infoText;
WWWForm form;
public void RegisterButton()
{
infoText.text = "Processing registration...";
}
public void LogginButton()
{
infoText.text = "Logging In...";
StartCoroutine(RequestLoggin());
}
IEnumerator RequestLoggin()
{
string email = inputEmail.text;
string password = inputPassword.text;
form = new WWWForm();
form.AddField("email", email);
form.AddField("password", password);
using (UnityWebRequest www = UnityWebRequest.Post("http://localhost:8888/action_login.php", form))
{
yield return www.SendWebRequest();
if (string.IsNullOrEmpty(www.error))
{
if (www.downloadHandler.text.Contains("invalid email or password"))
{
infoText.text = "invalid email or password";
}
else
{
infoText.text = "loggin success";
Debug.Log(www.downloadHandler.text);
}
}
else
{
infoText.text = "An error ocurred";
}
}
}
}
Your answer
Follow this Question
Related Questions
UnityWebRequest.Post() has multipart/form-data strangely formatted when sending request 5 Answers
UnityWebRequest returns an empty string 1 Answer
www request Error unity 2018 0 Answers
unity WebRequest - Windows security 0 Answers
WWW HTTP Post request only works on Android if i'm comnected to Wifi, not on cellular data 1 Answer