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

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器