If HttpClient is not supposed to be used in a using statement, see these links: https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/ http://www.nimaara.com/2016/11/01/beware-of-the-net-httpclient/
So if your not supposed to use HttpClient in a using statement then why do so many examples out there put WebClient in a using statement (including Microsoft); also I have not yet seen an article about not putting WebClient in a using statement? Eventually at the lowest level both WebClient and HttpClient end up at the same place opening a TCP/IP socket. Is it what's in-between that makes WebClient acceptable to put in a using statement?
Note I have a reason for asking this question: I have a client application that runs on WinXP, Win7 full and embedded systems, the code that I use to POST XML to my backend host (CentOS system) looks like this:
void PostXml(string url, string xml, int id) {
try
{
//Console.WriteLine("POST Request: " + xml);
using (WebClient wc = new WebClient())
{
wc.Proxy = null; //DEBUG: testing this to see if it helps webclient post failure after time x
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
Byte[] d = Encoding.ASCII.GetBytes(xml);
//however Async below is better regardless for closing app and non interference with GUI
//Byte[] res = web.UploadData(url, "POST", d); //sychrounous will block until sent and response returned
wc.UploadDataCompleted += new UploadDataCompletedEventHandler(web_UploadDataCompleted);
wc.UploadDataAsync(new Uri(url), "POST", d, id);
}
}
catch (WebException ex)
{
string responseText = "";
using (var reader = new StreamReader(ex.Response.GetResponseStream()))
{
responseText = reader.ReadToEnd();
}
string logMsg = DateTime.Now.ToString("s") + " - PostXml WebClient webexception: " + ex.Message + "\r\n" +
" response: " + responseText + "\r\n\r\n";
File.AppendAllText(SPUClient.frmMain._logFile, logMsg);
}
catch (Exception ex) {
//Console.WriteLine("PostXml Exception: " + ex.Message);
string logMsg = DateTime.Now.ToString("s") + " - PostXml exception: " + ex.Message + "\r\n";
File.AppendAllText(SPUClient.frmMain._logFile, logMsg);
}
}
PostXml is called 2 times every 1 minute, I have 400 clients in the field that run this code above, some of them have run for more than a year with no errors. But we have had a few clients that run this code after running for several weeks, or several months just stop doing the UploadDataAsync( "POST" ) evidently? All we know at this time is we don't get an exception, the POST message does not leave the computer and on my CentOS log file I can see that it never arrived (the domain.net.access.log on CentOS shows all the POST connections from all my clients). Remember this rarely happens some clients have never failed, some run for months / weeks before this happens. Restarting the client application fixes the problem of course. Should I be using WebClient in a using statement could that be the cause of something like this issue or is it something else?
来源:https://stackoverflow.com/questions/52787178/should-webclient-be-used-in-a-using-statement