C# 通过JSON将C Sharp(登录)连接到在线PHP插件(MySQL数据库)

C# 通过JSON将C Sharp(登录)连接到在线PHP插件(MySQL数据库),c#,php,json,C#,Php,Json,有谁能帮助我用PHP接收JSON POST请求的正确符号,以及用于发送POST请求的工作(并对齐)C Sharp脚本。我希望在我的PC上(使用C Sharp软件)进行分散登录,使用JSON POST请求连接到我的Web空间上的MySQL数据库(通过PHP7.1.1插件)。我可以看到脚本返回的异常(因此可以正确地接收JSON反馈),但PHP POST superglobal一直空着(在请求端) 我的C夏普脚本: using Newtonsoft.Json; using System;

有谁能帮助我用PHP接收JSON POST请求的正确符号,以及用于发送POST请求的工作(并对齐)C Sharp脚本。我希望在我的PC上(使用C Sharp软件)进行分散登录,使用JSON POST请求连接到我的Web空间上的MySQL数据库(通过PHP7.1.1插件)。我可以看到脚本返回的异常(因此可以正确地接收JSON反馈),但PHP POST superglobal一直空着(在请求端)

我的C夏普脚本:

using Newtonsoft.Json;
    using System;
    using System.IO;
    using System.Net;
    using System.Windows.Forms;


namespace HBI_Statistics_UI
{
    public partial class Login : Form
    {
        public class API_Response
        {
            public bool IsError { get; set; }
            public string ErrorMessage { get; set; }
            public string ResponseData { get; set; }
        }
    public Login()
    {
        InitializeComponent();
    }



    private void buttonLogin_Click(object sender, EventArgs e)
    {
        try
        {
            // request params
            var apiUrl = "http://www.home-business-intelligence.net/plugin/HBI-Statistics/login.php";



            var httpWebRequest = (HttpWebRequest)WebRequest.Create(apiUrl);
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {


                string json = "json=" + "{\"Email\":\"" + textBox1.Text + "\"," +
                                        "\"Pwd\":\"" + textBox2.Text + "\"}";

                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();

                API_Response r = JsonConvert.DeserializeObject<API_Response>(result);

                // check response
                if (r.ResponseData == "Success")
                {
                    this.Hide();
                    ProgramMainUI pr = new ProgramMainUI();
                    pr.Show();
                }
                else
                {
                    MessageBox.Show(r.ErrorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        catch (System.Net.WebException ex)
        {
            MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        catch (Newtonsoft.Json.JsonReaderException ne)
        {
            MessageBox.Show(ne.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}
使用(var-client=new-HttpClient())
{
var值=新字典
{
{“thing1”,“你好”},
{“thing2”,“world”}
};
var内容=新的FormUrlEncodedContent(值);
var response=wait client.PostAsync(“http://www.example.com/recepticle.aspx“,内容);
var responseString=await response.Content.ReadAsStringAsync();
}
include ("library.php");

try
    {
    if ($_SERVER["REQUEST_METHOD"] == "POST")
        {

        // Post Request

        $api_data = strip_tags(isset($_POST['json']) ? $_POST['json'] : '');

        // Validate Request

        if (empty($api_data))
            {
            throw new Exception('Invalid request!! Please provide login details.' . print_r($api_data));
            }

        if (!empty($api_data))
            {

            // Decode json

            $data = json_decode($api_data,  true);

            // Set connection

            $conn = Connection::Conn_2();

            // Sanitize data for query

            $pwd = mysqli_real_escape_string($conn, $data->json->Pwd);
            $email = mysqli_real_escape_string($conn, $data->json->Email);
            $pwd2 = VanillaSpecial::Hash($pwd); // Hashed Password

            // Insert Query of SQL

            $result = mysqli_query($conn, $sql = "SELECT * FROM `Management-employees` WHERE email='$email' AND password = '$pwd2'") or die(json_encode(array(
                'IsError' => 'true',
                'ErrorMessage' => 'Invalid Request!! Oops, something went wrong. Please try again!!'
            )));
            if (mysqli_num_rows($result) == 1)
                {

                // output data of each row

                while ($row = mysqli_fetch_assoc($result))
                    {
                    $functionCategory = $row[functionCategoryID];
                    }

                switch ($functionCategory)
                    {
                case "Management":

                    // echo "You have got clearance for this page!";

                    exit(json_encode(array(
                        'IsError' => 'false',
                        'ResponseData' => 'Success'
                    )));
                    break;

                default:
                    throw new Exception('Invalid clearance!!');
                    }
                }
              else
                {
                throw new Exception('ERROR: Could not be able to execute ' . $sql . ' - ' . mysqli_error($conn));
                }
            }
        }
      else
        {
        throw new Exception('Invalid access method!!');
        }
    }

catch(Exception $e)
    {
    exit(json_encode(array(
        'IsError' => 'true',
        'ErrorMessage' => $e->getMessage()
    )));
    }
using (var client = new HttpClient())
{
    var values = new Dictionary<string, string>
    {
       { "thing1", "hello" },
       { "thing2", "world" }
    };

    var content = new FormUrlEncodedContent(values);

    var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);

    var responseString = await response.Content.ReadAsStringAsync();
}