Consume Rest Webservice with json body request

This question already has an answer here:

  • How to post JSON to the server? 11 answers

  • 你必须使用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/45866.html

    上一篇: 使用JSON进行POST()

    下一篇: 使用json正文请求使用Rest Webservice