[英]Calling PHP Web -Service from C# Windows Form
I am trying to verify username, password, and software token number of a C# Windows Form to values in MySQL database.我正在尝试将 C# Windows 窗体的用户名、密码和软件令牌编号验证为 MySQL 数据库中的值。
My C# Code:我的 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);
}
My PHP code (Web service):我的 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;
?>
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.我正在努力了解如何从 C# 应用程序调用 Web 服务,如何将变量从 C# 应用程序传递到 Web 服务,以及如何从 Web 服务将 json_encode 返回到 C# 应用程序。
I am not a full-time programmer and this is my first encounter with web services.我不是全职程序员,这是我第一次接触 Web 服务。 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).
当我使用变量运行 php 代码时,它会运行并给我一个 $result (数组)。 A numeral answer 1.
数字答案1。
When I test my code to display the result in a MessageBox, the MessageBox is empty.当我测试我的代码以在 MessageBox 中显示结果时,MessageBox 为空。 Why ?
为什么 ?
Of course you can call WebService from C#.当然,您可以从 C# 调用 WebService。 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();
Code which I used:我使用的代码:
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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.