Im getting a strange error sometimes on my httpwebrequests.
Error im getting:
ArgumentException was unhandled by user code
'' is not a supported encoding name.
Code i am running:
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 3000;
request.ReadWriteTimeout = 3000;
request.Proxy = new WebProxy(p.ToString(), true);
response = (HttpWebResponse)request.GetResponse();
Encoding responseEncoding = Encoding.GetEncoding(response.CharacterSet);
using (StreamReader sr = new StreamReader(response.GetResponseStream(), responseEncoding))
{
if (response.StatusCode == HttpStatusCode.OK)
{
// do stuff
}
}
}
catch (WebException wexc1)
{
if (wexc1.Status == WebExceptionStatus.ProtocolError)
{
return false;
}
}
finally
{
if (response != null)
response.Close();
}
It doesent happend all the time its like 1 out of 500 requests. It feels like the code is unable to determine the actual encoding.. but how would i handle this?
The error currently makes the application crash
I assume that the exception is thrown in the line:
As a quick advice I would suggest setting
Encoding.UTF8orresponse.ContentEncodingas the encoding of the stream and not relying onresponse.CharacterSet().CharacterSet is a WebName (e.g.
Encoding.UTF8.WebName), not a proper name of the encoding.