从c#windows窗体调用php web服务

balp4ylt  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(325)

我正在尝试验证c#windows窗体的用户名、密码和软件令牌号是否与mysql数据库中的值一致。
我的c代码:

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;

?>

我正在努力理解如何从c应用程序调用web服务,如何将变量从c应用程序传递到web服务,以及如何将json编码从web服务返回到c应用程序。
我不是一个全职程序员,这是我第一次接触web服务。如果有任何关于如何改进这两个代码的建议,我将不胜感激。
更新
我已经更新了我的代码。当我用变量运行php代码时,它运行并给我一个$result(数组)。数字答案1。
当我测试代码以在messagebox中显示结果时,messagebox是空的。为什么?

vc6uscn9

vc6uscn91#

我使用的代码:

var username = txtusername.Text;
    var password = txtpassword.Text;
    string Token = "28956";
        var url = "https://mydomain.com.au/LoginVerification.php?";
        var var = "username=" + username + "&password=" + password + "&Token=" + Token ;
        var URL = url + var;
        //MessageBox.Show(URL);

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        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();
        //MessageBox.Show(responseFromServer);
        // Display the content.  
        if (responseFromServer == "\n  Allow")
        {
            MessageBox.Show("Success");           
        }
5f0d552i

5f0d552i2#

当然,您可以从c#调用webservice。有一个内置的计算系统。
单向:

WebRequest request = WebRequest.Create("http://localhost:8080/?username=john");
request.Method="GET";
WebResponse response = request.GetResponse();

其他方式:

HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:8080/");
HttpResponseMessage response = await client.PostAsJsonAsync( "api/user", userName);
response.EnsureSuccessStatusCode();

相关问题