使用Windows Phone Webclient C#的HTTP Post请求

时间:2014-08-28 07:55:12

标签: c# http post windows-phone-8

我的本​​地服务器上有一个php文件,如下所示:(数据库变量在config.php中并且是正确的!)

<?php

require_once "config.php";

try
{
    $con = new PDO("mysql:host=".$db_host.";dbname=".$db_name,$db_user,$db_password);
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
    echo "Error: ".$e->getMessage();
    exit();
}   

if (empty($_POST["number"]) || !isset($_POST["number"]))
{
    echo "Error: no number!";
    exit();
}
else if (empty($_POST["password"]) || !isset($_POST["password"]))
{
    echo "Error: no password!";
    exit();
}
else
{
    $number = $_POST["number"];
    $password = md5($_POST["password"]);
    $salt = sha1($_POST["password"]);
}

if (!empty($_POST["login"]))
{
    $sql = $con->prepare("SELECT COUNT(`ID`) FROM ".$db_table_login_students." WHERE `number` = ? AND `password` = ? AND `salt` = ? AND user_deleted=0");
    $sql->bindParam(1, $number);
    $sql->bindParam(2, $password);
    $sql->bindParam(3, $salt);
    $sql->execute();
    if($sql->fetchColumn() > 0) 
    {
        $login = array('Login' => 'Yes');
        echo json_encode($login);
        $_sql = $con->prepare("UPDATE ".$db_table_login_students." SET last_login=NOW() WHERE number = ?");
        $_sql->bindParam(1, $matrikelnummer);
        $_sql->execute();
        exit();
    } 
    else 
    {
        $login = array('Login' => 'No');
        echo json_encode($login);
        exit();
    }
}

?>

现在我想在C#中使用Windows Phone发布HTTP Post请求,现在看起来像这样:

void PostJsonRequestWebClient() 
    { 
        WebClient webclient = new WebClient(); 
        Uri uristring = null; 
        uristring = new Uri("http://localhost/login.php?"); 
        webclient.Headers["ContentType"] = "application/x-www-form-urlencoded"; 
        string WebUrlRegistration = ""; 
        string JsonStringParams = "login=yes&number=4340490&password=test"; 
        webclient.UploadStringCompleted += wc_UploadStringCompleted; 
        webclient.UploadStringAsync(uristring, "POST", JsonStringParams); 
    } 
    private void wc_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e) 
    { 
        try 
        { 

            if (e.Result != null) 
            { 
                string response = e.Result.ToString(); 
                textblock1.Text = response;
            } 
        } 
        catch 
        { 
        } 
    }

我的数据库中包含数字和密码的登录数据是正确的。当我发出Get请求而不是帖子时,我成了php脚本的回答&#34; {登录:是}&#34;。但当我发出像上面那样的帖子请求时,我变成了#34;错误:没有数字!&#34;。所以我认为Post查询字符串是false,但我什么都没发现。有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

也许,HTTP标头应该是Content-Type而不是ContentType