使用json正文请求使用Rest Webservice

这个问题在这里已经有了答案:

  • 如何发布JSON到服务器? 11个答案

  • 你必须使用StreamWriterStreamReader

    var httpWebRequest = (HttpWebRequest)WebRequest.Create(tb_URL.Text);
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "POST";
    
    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
    {
        string json = "{"lastName": "Jordan", "firstName": "Michael"}";
    
        streamWriter.Write(json);
        streamWriter.Flush();
        streamWriter.Close();
    }
    
    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var result = streamReader.ReadToEnd();
    }
    
    链接地址: http://www.djcxy.com/p/45865.html

    上一篇: Consume Rest Webservice with json body request

    下一篇: How to send json data in POST request using C#