Click here to Skip to main content
15,886,085 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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



C#
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();
    }
Posted
Updated 19-Aug-14 0:44am
v2

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.
 
Share this answer
 
I am having the same problem. Please help me too.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900