I have searched and can't find an answer that is close to what how I need to convert webClient to HttpClient.
I have an xml file that is written to a memory stream and uploaded to a provider to post data. I get back a response as a byte array that is converted to an ascii string. It is some code I copied 10 years ago and have been using it until I went to upgrade my webapi to net 6.0. Then I discovered that webClient was deprecated.
Here is the code I need to convert:
public static string Submit(string url,other string fields){
MemoryStream stream = new MemoryStream();
XmlTextWriter xml = new XmlTextWriter(stream, null);
--here I add several xml nodes and data
WebClient web = new WebClient();
web.Credentials = CredentialCache.DefaultCredentials;
try
{
byte[] response = web.UploadData(url, stream.ToArray());
return Encoding.ASCII.GetString(response);
}
catch (WebException ex)
{
if (((null != ex.Response)))
{
using (StreamReader reader = new StreamReader(ex.Response.GetResponseStream()))
{
return reader.ReadToEnd();
}
}
return ex.ToString();
}
}
Thanks in advance