C# Web Request Exception: The underlying connection was closed: An unexpected error occurred on a receive
Normally, this code works completely fine, but I have a single instance on a customers PC which is throwing this exception. We can't replicate at all. As in the subject, the exception I am getting is "The underlying connection was closed: An unexpected error occurred on a receive." I have researched the hell out of this but all I could find was setting KeepAlive to false, which I have already done. This is normally an SSL connection, but I have had the customer attempt the same connection over standard http and we got the same results.
It is a super fast web request usually, with the userinfo class having about 10 string elements, and the response in a string of about 100 characters after a tiny MySQL query. So I don't think this is a开发者_如何学运维 timeout thing.
Any ideas?
public static string HitServer(string url)
{
if (UserInfo.Instance == null) return "";
//Request update XML path
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.KeepAlive = false;
req.Method = "POST";
req.ContentType = "xml/text";
using (Stream reqStream = req.GetRequestStream())
{
XmlSerializer s = new XmlSerializer(typeof(UserInfo));
s.Serialize(reqStream, UserInfo.Instance);
}
WebResponse response = req.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader sr = new StreamReader(responseStream);
return sr.ReadToEnd();
}
After trying all kind of fixes like HTTP protocol changes, Expect100contine, keepalive and a handful of other request parameters, I had to switch to non-SSL and just encrypt the password over this connection.
Not the most ideal fix, but it had to be done. It seems that something on certain PC's would block SSL connections from our application. This was tested by using the browser view built into our software to attempt to load an image on a secure server - this failed.
When I get the "The underlying connection was closed: An unexpected error occurred on a receive.", it is usually caused by serialization and/or deserialization errors. I would guess that the problem occurs when deserializing the response on the client, if no errors are logged on the server.
How about registering a module to catch all errors on the client, and check if you get any errors?
精彩评论