如何从PHPWeb服务向c应用程序返回值

wtlkbnrh  于 2021-06-18  发布在  Mysql
关注(0)|答案(0)|浏览(177)

我有一个c#windows窗体应用程序。我将三个值(用户名、密码和令牌)发送到一个PHPWeb服务,以验证并登录到应用程序。
我的c#windows窗体应用程序代码:

private void btnlogin_Click(object sender, EventArgs e)
{
    if (String.IsNullOrEmpty(txtusername.Text))
    {
        MessageBox.Show("Please insert username");
    }

    if (String.IsNullOrEmpty(txtpassword.Text))
    {
        MessageBox.Show("Please insert password");
    }

    var username = txtusername.Text;
    var password = txtpassword.Text;
    string Token = "28956";
    var SoftwareToken = token;
    WebRequest request = WebRequest.Create("https://mydomain.com.au/Verification.php?username=username&password=password&Token=SoftwareToken");
    request.Method = "GET";
    WebResponse response = request.GetResponse();
    Console.WriteLine(((HttpWebResponse)response).StatusDescription);
    Stream dataStream = response.GetResponseStream();
    // Open the stream using a StreamReader for easy access.  
    StreamReader reader = new StreamReader(dataStream);
    // Read the content.  
    var responseFromServer = reader.ReadToEnd();
    responseFromServer.ToArray();
    /*I have tried:
    responseFromServer.ToArray();(because result on php page is an array.
    I have tried responseFromServer.ToString();*/
    MessageBox.Show(responseFromServer);
}

我的php web服务代码:

<?php
// Database Structure 
require_once('connect.php');

//Get password from the database for the user
$stmtus = $conn->prepare("SELECT password from `Users` where `email` = :Username");
$stmtus->bindParam(':Username', $username);
$username= $_GET['username'];;
$stmtus -> execute();
$password = $stmtus->fetch();

$un = $_GET['username'];
$pw = $_GET['password'];
$ust = $_GET['Token'];

if(password_verify($pw, $password[0])){
$stmt = $conn->prepare("SELECT 
COUNT(Token) AS cnt FROM `SoftwareToken` 
LEFT JOIN User ON iduser = SoftwareToken.Consultant 
WHERE Token = '$ust' 
AND username = '$un'");
$stmt->bindValue(':Username', $un);
$stmt->bindValue(':Token', $ust);
$stmt->execute();
$result= array();
while($SToken= $stmt->fetch(PDO::FETCH_OBJ)){
array_push($result, $SToken->cnt);  
}
echo json_encode($result);

}

$conn = null;

?>

好像我的代码在工作。我正试图以一种简单的方式显示结果 MessageBox.Show() 作为测试。最终目的是在if语句中使用返回值。返回值应该是0或1的数组。
if语句将是:

if (responseFromServer= 1)
        {
            Form1 identykidzform = new Form1();
            identykidzform.Show();

            Password loginform = new Password();
            loginform.Close();
            reader.Close();
            response.Close();
        }
        else
        {
            Application.Exit();
        }

当我打电话给 MessageBox.Show(responseFromServer) 它是空的。
请帮助我显示和使用返回变量。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题