繁体   English   中英

Windows Phone Webclient C#的HTTP发布请求

[英]HTTP Post Request with Windows Phone Webclient C#

我的本地服务器上有一个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发布请求,现在看起来像这样:

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请求而不是Post时,我成为php脚本“ {Login:Yes}”的答复。 但是当我像上面那样发出一个Post请求时,我变成了“错误:没有数字!”。 所以我认为Post查询字符串是假的,但是我什么也没找到。 有谁能够帮助我?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM