2013-02-01 85 views
2

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

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

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

对于PHP的一部分,我有:

php?fmessage=testtesttest" 

写:

<?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"; 
} 


?> 

在同一主题的用户也表示,尝试这种之一down输出使用

$name = $_GET["message"]; 
print $name; 

这样做不工作。难道我做错了什么?

感谢您的帮助已经

那么SOFAR我发现它不是发送值这就是错误的,但在获取值:

Username = Registry.CurrentUser.OpenSubKey("Username", true); 
Name = "" + Username.GetValue("Uid"); 
它说,该值是REG_BINARY注册表编辑菜单

,这些与getvalue可读?

+2

你能否详细说明它是如何不起作用一点?究竟会发生什么? –

+0

您可能没有发送到正确的PHP文件,请确保您正在写入php文件的整个路径,因为用户的回复想要检查您是否正在寻址到正确的页面。 –

+0

@MrLister我尝试发送一个字符串,我在C#(Visual Studio 10)中获得字符串url =“http://melloniax.com/receive.php?message=testtesttest”;该消息是为我测试的测试消息,我想要的PHP页面获取该字符串,并通过querry发送到一个php表格 –

回答

6

使用此代码为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"; 
    } 

} 

?>