【问题标题】:C# Sending POST To PHP MYSQL but not recieving anythingC# 向 PHP MYSQL 发送 POST 但没有收到任何内容
【发布时间】:2013-10-04 17:47:15
【问题描述】:

正如我在图块中所说的......这是我所有的代码 我的 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']);
    }

?>

这是我的 C# 代码

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();
    }
  }
}

请有人对此有所了解并帮助我修复和完善脚本,请帮助。 任何帮助都会非常有用:)

【问题讨论】:

  • 您是否使用过Fiddler 之类的方法来验证数据是否实际发送正确?请解决那些SQL Injection 错误
  • 将 echo $var_dump($_POST) 放在 &lt;?php 之后,以便查看问题的更多详细信息。

标签: c# php mysql


【解决方案1】:

实用又简单的示例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();

【讨论】:

  • 用户名和密码是php中定义的POST参数!
猜你喜欢
  • 2018-01-25
  • 1970-01-01
  • 2014-01-23
  • 2016-10-17
  • 2011-03-26
  • 1970-01-01
  • 1970-01-01
  • 2017-05-05
  • 2017-05-29
相关资源
最近更新 更多