doutang7661 2018-12-05 12:51
浏览 165
已采纳

从C#Windows窗体调用PHP Web服务

I am trying to verify username, password, and software token number of a C# Windows Form to values in MySQL database.

My C# Code:

 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);
    }

My PHP code (Web service):

<?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;

?>

I am battling to understand how I call the web service from the C# application, how do I pass the variables from the C# application to the web service and how do I return the json_encode to the C# application from the web service.

I am not a full-time programmer and this is my first encounter with web services. If there are any suggestions on how to improve either of the codes, I would much appreciate.

UPDATE

I have updated my code as assisted. When I run the php code with variables it runs and gives me a $result (array). A numeral answer 1.

When I test my code to display the result in a MessageBox, the MessageBox is empty. Why ?

  • 写回答

2条回答 默认 最新

  • drh19790711 2018-12-05 13:04
    关注

    Of course you can call WebService from C#. There is a built in calss in System.

    One Way:

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

    Other Way:

    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri("http://localhost:8080/");
    HttpResponseMessage response = await client.PostAsJsonAsync( "api/user", userName);
    response.EnsureSuccessStatusCode();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵