WebRequest和标签数​​据

时间:2013-08-06 15:59:24

标签: c#

我在PHP-MySql上创建了授权,它运行正常。所有这些都破坏了我认为的TextBox。这是我的PHP代码。没有错误。

<?php
include('../functions.php'); // connect mysqli
if ($_POST['form_name'] == 'loginform') {
  $crypt_pass = md5($_POST['password']);
  $found = false;

  $res = $mysqli->query("SELECT password FROM accounts WHERE username = '".$mysqli->real_escape_string($_POST['username'])."'");
  if ($data = $res->fetch_array()) {
    if ($crypt_pass == $data['password']) {
      $found = true;
    }
  }
  if ($found == false) {
    echo 'LoginFail';
  }
  else {
    echo 'LoginOK';
  }
}
?>

我的WebRequest:

 WebRequest request = WebRequest.Create("http://unknow.com/user/login2.php");
 request.Method = "POST";
 string postData = "form_name=loginform&username=" + LoginInput + 
      "&password=" + PasswordInput;
 byte[] byteArray = Encoding.UTF8.GetBytes(postData);
 request.ContentType = "application/x-www-form-urlencoded";
 request.ContentLength = byteArray.Length;
 Stream dataStream = request.GetRequestStream();
 dataStream.Write(byteArray, 0, byteArray.Length);
 dataStream.Close();
 WebResponse response = request.GetResponse();
 dataStream = response.GetResponseStream();
 StreamReader reader = new StreamReader(dataStream);
 string responseFromServer = reader.ReadToEnd();
 Console.WriteLine(responseFromServer);
 response_label.Text = responseFromServer;
 reader.Close();
 dataStream.Close();
 response.Close();

当这段代码没问题时,PHP告诉我“LoginOK”:

string postData="form_name=loginform&username=snosme&password=123456";

但是当代码:

string postData = "form_name=loginform&username=" + LoginInput + 
      "&password=" + PasswordInput;

PHP告诉我LoginFail。虽然我在TextBox中输入相同的数据。有什么问题?

1 个答案:

答案 0 :(得分:0)

看到你的图像后,我认为问题是你忘记在文本字段中获取Text值。试试这个:

string postData = "form_name=loginform&username=" + LoginInput.Text + 
  "&password=" + PasswordInput.Text;