C#发送POST给PHP MYSQL但没有收到任何东西

时间:2013-10-04 17:47:15

标签: c# php mysql

正如我在瓷砖中说的那样.......这是我所有的代码 我的PHP脚本

<?php
    $DGURL = $_POST["url"];
    $DGUSER = $_POST["user"];
    $DGPASS = $_POST["pass"];
    function db_connect() {
        $hostname = '127.0.0.1';
        $db_user = 'root';
        $db_password = '';
        $db_name = 'hit';

        $conn = mysql_connect ($hostname, $db_user, $db_password) or 
            die (mysql_error()); 
        echo "Success.. Connected to MySQL...<br />"; 
        mysql_select_db($db_name) or die(mysql_error()); 
        echo "Success.. Connected to Database...<br /> "; 

        return $conn;
    }

    $conn = db_connect();

    function insertData($DGURL, $DGUSER, $DGPASS) {
        $requete = "INSERT INTO data SET 
            Url='".$DGURL."', 
            Username='".$DGUSER."', 
            Password='".$DGPASS."'";
        mysql_query($requete) or die(mysql_error());
    }

    if(isset($_GET['DGURL']) && isset($_GET['DGUSER']) && isset($_GET['DGPASS'])) {
        insertData($_GET['DGURL'], $_GET['DGUSER'], $_GET['DGPASS']);
    }

?>

HERES MY C#CODE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Collections.Specialized;
using System.Net;
using System.IO;


namespace ConsoleApplication1
{
  class Program
  {
     static void Main(string[] args)
     {
        string URL = "http://localhost/test.php";
        WebClient webClient = new WebClient();

        NameValueCollection formData = new NameValueCollection();
        formData["url"] = "url";
        formData["user"] = "user";
        formData["user"] = "pass";

        byte[] responseBytes = webClient.UploadValues(URL, "POST", formData);
        string responsefromserver = Encoding.UTF8.GetString(responseBytes);
        Console.WriteLine(responsefromserver);
        webClient.Dispose();
    }
  }
}

有人可以对此有所了解并帮助我修复和修改脚本请帮忙。 任何帮助都将非常有用:)

1 个答案:

答案 0 :(得分:0)

有用且简单的示例here

将此代码放入表单加载,按钮单击等操作中

     string URL = "http://www.softwareandfinance.com/PHP/portal_welcome.php";
     WebClient webClient = new WebClient();

     NameValueCollection formData = new NameValueCollection();
     formData["username"] = "testuser";
     formData["password"] = "mypassword";

     byte[] responseBytes = webClient.UploadValues(URL, "POST", formData);
     string responsefromserver = Encoding.UTF8.GetString(responseBytes);
     Console.WriteLine(responsefromserver);
     webClient.Dispose();