0

May I know what the difference between using WebRequest and HttpPost attribute? I am trying to get the response from the 3rd party API using the WebRequest. And currently, I am able to retrieve the response API. Please enlighten me between the WebRequest and HttpPost. I am still new to this kind of task.

WebRequest requestObjPost = WebRequest.Create(baseURL);
requestObjPost.Method = "POST";
requestObjPost.ContentType = "application/json";

string postData = "{\"title\":\"testdata\",\"body\":\"testbody\",\"userId\":\"50\"}";
using (var streamWriter = new StreamWriter(requestObjPost.GetRequestStream()))
{
    streamWriter.Write(postData);
    streamWriter.Flush();
    streamWriter.Close();

    var httpResponse = (HttpWebResponse)requestObjPost.GetResponse();

    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var result = streamReader.ReadToEnd();
        Console.WriteLine(result);
    }
}
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Flickery
  • 1
  • 1
  • Does this help? https://stackoverflow.com/questions/27793761/httpclient-vs-httpwebrequest-for-better-performance-security-and-less-connectio – HsuTingHuan Oct 15 '22 at 17:50

0 Answers0