locked
a runnable demo will be perfect RRS feed

  • Question

  • hi,


    i want to request some json data by using WebRequest, I tried a lot of time,but it always has some exception ,can some one give me a demo which can run ,not pieces of code,so it may can save me a lot of time . i want to know how yo guys gain the json data.

    Tuesday, April 29, 2014 12:44 PM

Answers

  • Hi,

    For your requirement, please try to refer to the following code:

    protected void Button1_Click(object sender, EventArgs e)
            {
                var webAddr = "http://localhost:11432/Service1.svc/GetName";
                var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
                httpWebRequest.ContentType = "application/json; charset=utf-8";
                httpWebRequest.Method = "POST";
    
                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    string json = "{\"name\":\"good\"}";
    
                    streamWriter.Write(json);
                    streamWriter.Flush();
                }
    
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    string result = streamReader.ReadToEnd();
                    Button1.Text = result;
                }
            }

    Best Regards,
    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.


    Thursday, May 1, 2014 8:11 AM
    Moderator