2017-02-05 4 views
0

PHPでJSON POSTリクエストを受け取り、POSTを送信するために動作する(整列した)Cシャープスクリプトを正しく表記できる人は誰でも助けてください要求。私は私のウェブスペース上で(PHP 7.1.1プラグインを介して)私のMySQLデータベースに接続するためにJSON POST要求を使用する私のPC(C Sharpソフトウェアを使用)で分散ログインをしたい。スクリプトが返す例外(JSONフィードバックが適切に受信される)が表示されますが、PHP POSTのスーパーグローバルは空のまま(リクエスト側で)持ち続けます。JSONでオンラインPHPプラグイン(MySQLデータベース)にCシャープ(ログイン)を接続

私の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); 
     } 
    } 
} 

私のPHPスクリプト:

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

答えて

0
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(); 
} 
関連する問題