dqz86173 2017-03-29 16:18
浏览 300

如何从c#通过localhost发布json并获取php中的数据?

I'm trying to do a simple program with a very simple UI.

Just a button in a c# windows form, when i press the button it encodes a simple array to Json and upload it to http://localhost, and then get the json data in PHP.

This is my code now in c#

private void button_Click(object sender, RoutedEventArgs e)
{
    var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:8080/SemesterProject/index.php");
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "POST";

    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
    {
        string json = new JavaScriptSerializer().Serialize(new
        {
            user = "Foo",
            password = "Baz"
        });

        streamWriter.Write(json);
        Console.WriteLine("Console.WriteLine(json);");
        Console.WriteLine(json);

    }


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

    }
}

And this is my code in PHP:

<?php
$post= file_get_contents("php://input");
$decodedjson=json_decode($post,true);
var_dump($decodedjson);

?>

at PHP output i get 'NULL' and in c# output i get

Console.WriteLine(json);
{"user":"Foo","password":"Baz"}
Console.WriteLine(result);
array(2) {
  ["user"]=>
  string(3) "Foo"
  ["password"]=>
  string(3) "Baz"
}

After this i need to post the data from PHP to phpMyAdmin database

  • 写回答

1条回答 默认 最新

  • dousaoxiancy199896 2017-03-29 16:28
    关注

    Try simple way:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections.Specialized;
    using System.Net;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
        public Form1()
            {
            InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
    
            using (var client = new WebClient())
                {
                var postData = new NameValueCollection();
                postData["user"] = "Foo";
                postData["password"] = "Baz";
                var response = client.UploadValues("http://localhost/xxx.php", postData);
                var data = Encoding.Default.GetString(response);
                // Console.WriteLine("Data from server: " + data);
                MessageBox.Show(data);
                }
    
            }
        }
    }
    

    // php file xxx.php

    <?php
    echo json_encode($_POST);
    ?>
    

    In your message box will dosplay json string

    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大