如何测试UnityWebRequest的结果?
How to test result of UnityWebRequest?
我正在尝试在 Unity 中制作游戏原型。我有一个 MySQL 数据库,我可以在其中注册一个用户。
所以我遇到的麻烦是每当我填写错误的字段时收到错误消息。就像在发送 SQL 请求然后加载新场景之前需要匹配的两个密码字段。
我已经尝试过 if(www.downloadHandler.text == "")
但如果我不填写信息字段,我的测试会通过,因为它 return 和 ""
这是我的 C# 文件:
IEnumerator Insertion()
{
WWWForm form = new WWWForm();
form.AddField("Identifiant", IdField.text);
form.AddField("MotPasse", MdpField.text);
form.AddField("MotPasseConfi", MdpConfirField.text);
form.AddField("Email", EmailField.text);
form.AddField("NoTel", NoTelField.text);
UnityWebRequest www = UnityWebRequest.Post("http://localhost/AppAndroid/inscription.php", form);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
// It doesn't works, show error message
Debug.Log(www.error);
Debug.Log(www.downloadHandler.text);
}
else
{
// It works, load new scene
Debug.Log("c'est passé !");
Debug.Log(www.downloadHandler.text);
}
}
这是我的 php 文件:
<?php
$SRVusername="root";
$SRVpassword="";
$SRVadress="localhost";
$SRVdatabase="envapp";
$connexion=mysqli_connect($SRVadress,$SRVusername,$SRVpassword,$SRVdatabase);
if(mysqli_connect_errno())
{
echo "1: Erreur de connexion";
exit();
}
if(isset($_POST["Identifiant"]) && isset($_POST["MotPasse"]) && isset($_POST["Email"]) && isset($_POST["NoTel"]) && isset($_POST["MotPasseConfi"]))
{
$Identifiant = $_POST["Identifiant"];
$MotPasse = $_POST["MotPasse"];
$Email = $_POST["Email"];
$NoTel = $_POST["NoTel"];
$MotPasseConfi = $_POST["MotPasseConfi"];
if($MotPasse == $MotPasseConfi)
{
$checkquery = "INSERT INTO `joueurs` (`ID_JOUEUR`, `PSEUDO`, `PASSWORD`, `EMAIL`, `NUMTEL`) VALUES (NULL, '".$Identifiant."', '".$MotPasse."', '".$Email."', '".$NoTel."');";
$checkstate = mysqli_query($connexion,$checkquery) or die("4: Erreur lors de la requête " .$checkquery);
}
else
{
echo "3: Erreur Les mots de passe ne correspondent pas !";
exit();
}
}
else
{
echo "2: Erreur Les POST ne sont pas passés !";
exit();
}
mysqli_close($connexion);
?>
我建议使用例如http_response_code
to set a proper http response code (like 500
- see list of http response codes),而是使用这些进行测试:
<?php
$SRVusername="root";
$SRVpassword="";
$SRVadress="localhost";
$SRVdatabase="envapp";
$connexion=mysqli_connect($SRVadress,$SRVusername,$SRVpassword,$SRVdatabase);
if(mysqli_connect_errno())
{
printf("1: Erreur de connexion\n");
http_response_code(500);
exit();
}
if(isset($_POST["Identifiant"]) && isset($_POST["MotPasse"]) && isset($_POST["Email"]) && isset($_POST["NoTel"]) && isset($_POST["MotPasseConfi"]))
{
$Identifiant = $_POST["Identifiant"];
$MotPasse = $_POST["MotPasse"];
$Email = $_POST["Email"];
$NoTel = $_POST["NoTel"];
$MotPasseConfi = $_POST["MotPasseConfi"];
if($MotPasse == $MotPasseConfi)
{
$checkquery = "INSERT INTO `joueurs` (`ID_JOUEUR`, `PSEUDO`, `PASSWORD`, `EMAIL`, `NUMTEL`) VALUES (NULL, '".$Identifiant."', '".$MotPasse."', '".$Email."', '".$NoTel."');";
if($checkstate = mysqli_query($connexion,$checkquery))
{
http_response_code(200);
} else {
printf("4: Erreur lors de la requête %s\n", $checkquery);
http_response_code(500);
exit();
}
}
else
{
printf("3: Erreur Les mots de passe ne correspondent pas !");
http_response_code(500);
exit();
}
}
else
{
printf("2: Erreur Les POST ne sont pas passés !");
http_response_code(500);
exit();
}
mysqli_close($connexion);
?>
那你就可以完全依靠
if (www.isNetworkError || www.isHttpError)
{
// It doesn't works, show error message
Debug.Log(www.error);
Debug.Log(www.downloadHandler.text);
}
或者简单地检查响应代码,如
if(www.responseCode != 200) { ... }
或者,对于自定义回复文本,您也可以使用 header,例如
header($_SERVER["SERVER_PROTOCOL"]. ' ' . 500 . ' ' . "1: Erreur de connexion");
我不知道你会如何回应 PHP 但我的回答是你 return 一个相关的错误代码(也许 412 - Precondition Failed in your case). This would allow you to check the .isHttpError
property of the UnityWebRequest
; see here.
客户端可以解释响应代码并显示错误(以正确的语言),或者您可以将 header 添加到包含详细信息的响应中。后者可以通过 .GetResponseHeader
property.
访问
但是,我建议您在发送请求之前验证您在 Unity 客户端上可以做什么。可以轻松检查密码匹配的简单验证,避免您发送明知会失败的请求。
我正在尝试在 Unity 中制作游戏原型。我有一个 MySQL 数据库,我可以在其中注册一个用户。
所以我遇到的麻烦是每当我填写错误的字段时收到错误消息。就像在发送 SQL 请求然后加载新场景之前需要匹配的两个密码字段。
我已经尝试过 if(www.downloadHandler.text == "")
但如果我不填写信息字段,我的测试会通过,因为它 return 和 ""
这是我的 C# 文件:
IEnumerator Insertion()
{
WWWForm form = new WWWForm();
form.AddField("Identifiant", IdField.text);
form.AddField("MotPasse", MdpField.text);
form.AddField("MotPasseConfi", MdpConfirField.text);
form.AddField("Email", EmailField.text);
form.AddField("NoTel", NoTelField.text);
UnityWebRequest www = UnityWebRequest.Post("http://localhost/AppAndroid/inscription.php", form);
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
// It doesn't works, show error message
Debug.Log(www.error);
Debug.Log(www.downloadHandler.text);
}
else
{
// It works, load new scene
Debug.Log("c'est passé !");
Debug.Log(www.downloadHandler.text);
}
}
这是我的 php 文件:
<?php
$SRVusername="root";
$SRVpassword="";
$SRVadress="localhost";
$SRVdatabase="envapp";
$connexion=mysqli_connect($SRVadress,$SRVusername,$SRVpassword,$SRVdatabase);
if(mysqli_connect_errno())
{
echo "1: Erreur de connexion";
exit();
}
if(isset($_POST["Identifiant"]) && isset($_POST["MotPasse"]) && isset($_POST["Email"]) && isset($_POST["NoTel"]) && isset($_POST["MotPasseConfi"]))
{
$Identifiant = $_POST["Identifiant"];
$MotPasse = $_POST["MotPasse"];
$Email = $_POST["Email"];
$NoTel = $_POST["NoTel"];
$MotPasseConfi = $_POST["MotPasseConfi"];
if($MotPasse == $MotPasseConfi)
{
$checkquery = "INSERT INTO `joueurs` (`ID_JOUEUR`, `PSEUDO`, `PASSWORD`, `EMAIL`, `NUMTEL`) VALUES (NULL, '".$Identifiant."', '".$MotPasse."', '".$Email."', '".$NoTel."');";
$checkstate = mysqli_query($connexion,$checkquery) or die("4: Erreur lors de la requête " .$checkquery);
}
else
{
echo "3: Erreur Les mots de passe ne correspondent pas !";
exit();
}
}
else
{
echo "2: Erreur Les POST ne sont pas passés !";
exit();
}
mysqli_close($connexion);
?>
我建议使用例如http_response_code
to set a proper http response code (like 500
- see list of http response codes),而是使用这些进行测试:
<?php
$SRVusername="root";
$SRVpassword="";
$SRVadress="localhost";
$SRVdatabase="envapp";
$connexion=mysqli_connect($SRVadress,$SRVusername,$SRVpassword,$SRVdatabase);
if(mysqli_connect_errno())
{
printf("1: Erreur de connexion\n");
http_response_code(500);
exit();
}
if(isset($_POST["Identifiant"]) && isset($_POST["MotPasse"]) && isset($_POST["Email"]) && isset($_POST["NoTel"]) && isset($_POST["MotPasseConfi"]))
{
$Identifiant = $_POST["Identifiant"];
$MotPasse = $_POST["MotPasse"];
$Email = $_POST["Email"];
$NoTel = $_POST["NoTel"];
$MotPasseConfi = $_POST["MotPasseConfi"];
if($MotPasse == $MotPasseConfi)
{
$checkquery = "INSERT INTO `joueurs` (`ID_JOUEUR`, `PSEUDO`, `PASSWORD`, `EMAIL`, `NUMTEL`) VALUES (NULL, '".$Identifiant."', '".$MotPasse."', '".$Email."', '".$NoTel."');";
if($checkstate = mysqli_query($connexion,$checkquery))
{
http_response_code(200);
} else {
printf("4: Erreur lors de la requête %s\n", $checkquery);
http_response_code(500);
exit();
}
}
else
{
printf("3: Erreur Les mots de passe ne correspondent pas !");
http_response_code(500);
exit();
}
}
else
{
printf("2: Erreur Les POST ne sont pas passés !");
http_response_code(500);
exit();
}
mysqli_close($connexion);
?>
那你就可以完全依靠
if (www.isNetworkError || www.isHttpError)
{
// It doesn't works, show error message
Debug.Log(www.error);
Debug.Log(www.downloadHandler.text);
}
或者简单地检查响应代码,如
if(www.responseCode != 200) { ... }
或者,对于自定义回复文本,您也可以使用 header,例如
header($_SERVER["SERVER_PROTOCOL"]. ' ' . 500 . ' ' . "1: Erreur de connexion");
我不知道你会如何回应 PHP 但我的回答是你 return 一个相关的错误代码(也许 412 - Precondition Failed in your case). This would allow you to check the .isHttpError
property of the UnityWebRequest
; see here.
客户端可以解释响应代码并显示错误(以正确的语言),或者您可以将 header 添加到包含详细信息的响应中。后者可以通过 .GetResponseHeader
property.
但是,我建议您在发送请求之前验证您在 Unity 客户端上可以做什么。可以轻松检查密码匹配的简单验证,避免您发送明知会失败的请求。