I would like to update some legacy code from using HttpWebRequest to use HttpClient, but I am not quite sure how to send string to the REST API I am accessing.
Legacy code:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "text/xml";
request.ContentLength = payload.Length;
if (credentials != null)
{
request.Credentials = credentials;
}
// Send the request
Stream requestStream = request.GetRequestStream();
requestStream.Write(payload, 0, payload.Length);
requestStream.Close();
// Get the response
response = (HttpWebResponse)request.GetResponse();
Can I use the HttpClient.GetStreamAsync method and use the stream like we did with the web request? Or is there a way to use SendAsync with content and then get the response?