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);
}
}