locked
wcf rest json, to get the postback data in http post request RRS feed

  • Question

  • hi, experts, I have a wcf rest json server for responding the http request from android and ios

    private void button1_Click(object sender, EventArgs e)
            {
                var httpWebRequest = (HttpWebRequest)WebRequest.Create(@"http://localhost:52450/Mobile/postbackuri");
                httpWebRequest.ContentType = "text/json";
                httpWebRequest.Method = "POST";
                
                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    string json = "{" +                            
                                "\"DeviceModel\":\"\"," +                            
                                "\"Password\":\"passw0rd\"," +                                                        
                                "\"UID\":\"johnChan\"" +                    
                                "}";
    
                    streamWriter.Write(json);
                    streamWriter.Flush();
                    streamWriter.Close();
    
                    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        var result = streamReader.ReadToEnd();
                        this.textBox1.Text = result.ToString();
                    }                
                }           
            }
    

    above code is from a webform application to make http post request to the wcf rest json server.

    how can I get the post data -> json string in wcf rest json server? I need to do some troubleshooting for the post data.

    thank you very much!

    Tuesday, April 1, 2014 5:24 AM

Answers

  • Hi,

    >>how can I get the post data -> json string in wcf rest json server? I need to do some troubleshooting for the post data.

    It seem that you want to retrieve the Request Body in a WCF RESTful Service, then please try to check the following code snippet to see if it helps:

    public Stream getJsonRequest() { // Get the raw json POST content. .Net has this in XML string.. string JSONstring = OperationContext.Current.RequestContext.RequestMessage.ToString(); // Parse the XML string into a XML document XmlDocument doc = new XmlDocument(); doc.LoadXml(JSONstring); foreach (XmlNode node in doc.DocumentElement.ChildNodes) { node.Name // has key node.InnerText; // has value

    }

    .........


    Best Regars,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, April 2, 2014 2:55 AM