【问题标题】:send C# string to .PHP page将 C# 字符串发送到 .PHP 页面
【发布时间】:2013-01-16 22:12:24
【问题描述】:

看了一圈后,我来到了this page。在这里,我找到了一些将 C# 字符串发送到 PHP 页面的代码。

但是,在我自己的程序中实现它之后,它不起作用。这是我的代码:

        private void executesend()
        {  
            using (WebClient client = new WebClient())
            {
                client.UploadString(url,"POST",keys);
            }
        }

对于 PHP 部分,我有:

    <?php 
    mysql_connect("localhost", "", "") or die(mysql_error()); // Connect to database server(localhost) with username and password.
            mysql_select_db("dimittv89_dayz") or die(mysql_error()); // Select registration database.
            $name = $_GET["message"];
 if ( $_SERVER['REQUEST_METHOD'] == 'POST' )
{
    $name = file_get_contents('php://input');

    $opdracht = "INSERT INTO 'keys', (`key`) VALUES ('$name')";
    print $name;
}

if (mysql_query($opdracht)){ 
echo "succesfully registerd to the melloniax u wil now be returned to our main page";
 }
 else{
 echo "hey something went wrong there ! please try again in a minute";
 }


 ?>

在同一主题中,一位用户还说试试这个:

php?fmessage=testtesttest"

并使用

写下输出
$name = $_GET["message"];
print $name;

这也不起作用。我做错了吗?

已经感谢您的帮助

到目前为止,我发现错误的不是发送值而是获取值:

Username = Registry.CurrentUser.OpenSubKey("Username", true);
Name = "" + Username.GetValue("Uid");

在 regedit 菜单中它说该值为 REG_BINARY,这些值是否可以使用 getvalue 读取?

【问题讨论】:

  • 你能详细说明一下它是如何不起作用的吗?究竟会发生什么?
  • 您可能没有发送到正确的 php 文件,请确保您正在编写 php 文件的整个路径,因为回复的用户想检查您是否正在发送到正确的页面.
  • @MrLister 我尝试将我在 C# (visual studio 10) 中获得的字符串发送到字符串 url = "melloniax.com/receive.php?message=testtesttest";该消息用于我尝试的测试消息,我希望 php 页面获取该字符串并通过查询将其发送到 php 表
  • 你能包含更多你的代码吗?在我看来,您可能在代码中存在流控制问题。
  • 我猜测SQL语句不正确。您可以将client.UploadString(url,"POST",keys); 的返回字符串添加到答案中吗?您应该能够调试并捕获函数的返回值。

标签: c# php mysql


【解决方案1】:

将此代码用于 c# 和 php:

private void executesend()
    {  

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

                req.Method = "POST";
                string Data = "message="+keys;
                byte[] postBytes = Encoding.ASCII.GetBytes(Data);
                req.ContentType = "application/x-www-form-urlencoded";
                req.ContentLength = postBytes.Length;
                Stream requestStream = req.GetRequestStream();
                requestStream.Write(postBytes, 0, postBytes.Length);
                requestStream.Close();

                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                Stream resStream = response.GetResponseStream();

                var sr = new StreamReader(response.GetResponseStream());
                string responseText = sr.ReadToEnd();


            }
            catch (WebException)
            {

                MessageBox.Show("Please Check Your Internet Connection");
            }

}

和php

<?php 
    mysql_connect("localhost", "", "") or die(mysql_error()); // Connect to database server(localhost) with username and password.
            mysql_select_db("dimittv89_dayz") or die(mysql_error()); // Select registration database.

 if (isset($_POST['message']))
{
    $name = $_POST['message'];

    $opdracht = "INSERT INTO keys (key) VALUES ('$name')";
    print $name;
    if (mysql_query($opdracht)){ 
     echo "succesfully registerd to the melloniax u wil now be returned to our main page";
    }
    else{
     echo "hey something went wrong there ! please try`enter code here` again in a minute";
    }

}

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-31
    • 1970-01-01
    • 2010-10-13
    相关资源
    最近更新 更多