c#使用HttpWebRequest登录

本文关键字:登录 HttpWebRequest 使用 | 更新日期: 2023-09-27 18:30:09

我希望有一个程序可以连接到需要用户ID和密码的网页,我用了下面的代码来做到这一点。但不知怎么的,它失败了

代码:PHP文件

login.php

$userid= $_POST['userid'];
$password= $_POST['password'];
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
if (mysqli_query("SELECT userid,password FROM aster_users WHERE userid = '$userid' AND password = '$password'")) {
    echo "login successfully";
} else {
    echo "Login failed: " . mysqli_error($conn);
}

C#

 private void btnLogin_Click(object sender, EventArgs e)
        {
  var request = (HttpWebRequest)WebRequest.Create("http://10.0.589.124/login.php");
            request.Method = WebRequestMethods.Http.Post;
            request.ContentType = "application/x-www-form-urlencoded";
            using (var stream = request.GetRequestStream())
            {
                var buffer = Encoding.UTF8.GetBytes("userid= " + txtusername.Text + " & password= " + txtpassword.Text +"");
                stream.Write(buffer, 0, buffer.Length);
            }
            var response = (HttpWebResponse)request.GetResponse();
            string result = String.Empty;
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                result = reader.ReadToEnd();
            }
            txtmessage.Text = result;
}

我在这里做错了什么?任何帮助请,非常感谢!:)

c#使用HttpWebRequest登录

试试这个:

$userid= $_POST['userid'];
$getpassword= $_POST['password'];
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT userid,password FROM aster_users WHERE userid = '$userid' AND password = '$getpassword'";
$result = mysqli_query($conn, $sql);
$count=mysqli_num_rows($result);
if($count==1){
    echo "login successfully";
} else {
    echo "Login failed" . mysqli_error($conn);
}