如何将c#中的json数据发送到php [英] how send json data from c# to php

查看:284
本文介绍了如何将c#中的json数据发送到php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将json数据从asp.net发送到php,但是我正在使用的代码我正在使用urland获取服务器响应但不向服务器发送数据。



如果数据发送成功,从服务器返回的消息将是:数据到达

如果数据未成功发送,则从服务器返回消息将是:加载json数据失败



i我运行代码时遇到第二个错误。我的代码在下面







I am trying to send json data from asp.net to php but code i which i am using is hitting urland getting server response but not sending data to server.

If data sent successfully, return message from server will be: Data reached
If data not sent successfully, return message from server will be: failed loading json data

i am getting second error when i am running my code. My code is below



protected void Button2_Click1(object sender, EventArgs e)
  
{
        
HttpWebRequest request=HttpWebRequest)WebRequest.Create("http://www.invoicera.com/testbeta/test.php");

        request.Method = "POST";
        request.ContentType = "application/json; charset=utf-8";
        string data = "{\"user\":\"test\",\"name\":\"xyz\"}";
        DataContractJsonSerializer ser = new DataContractJsonSerializer(data.GetType());
        MemoryStream ms = new MemoryStream();
        ser.WriteObject(ms, data);
        String json = Encoding.UTF8.GetString(ms.ToArray());
        StreamWriter writer = new StreamWriter(request.GetRequestStream());
        writer.Write(json);
        writer.Close();
        WebResponse response = request.GetResponse();
        // Get the stream containing content returned by the server.
        Stream dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Display the content.
        Response.Write(responseFromServer);
        // Clean up the streams and the response.
        reader.Close();
        response.Close();
    }

推荐答案

我最好的猜测是你发送的json字符串不是你想要的。



您的初始字符串数据已经是有效的json,然后将其序列化为json,我希望它只是给你一个未命名的字符串。根据服务器的设置方式,它可能会完全忽略您发送的json,因为它不包含任何预期的变量。
My best guess is that the json string you are sending is not what you wanted.

You initial string data is already valid json, you then serialize that into json which I expect just gives you an unnamed string. Depending on how the server is set up it may then be completely ignoring the json you sent because it doesn't contain any variables it was expecting.


我遇到了同样的问题。请帮助我。
I am having the same problem. Please help me too.


这篇关于如何将c#中的json数据发送到php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆