C# Web 请求异常:底层连接已关闭:接收时发生意外错误
通常,此代码完全可以正常工作,但我在客户 PC 上有一个实例抛出此异常。我们根本无法复制。正如主题所示,我收到的异常是“底层连接已关闭:接收时发生意外错误。”我已经对此进行了研究,但我所能找到的就是将 KeepAlive 设置为 false,我已经这样做了。这通常是 SSL 连接,但我让客户尝试通过标准 http 进行相同的连接,我们得到了相同的结果。
通常这是一个超快的 Web 请求,userinfo 类大约有 10 个字符串元素,在一个微小的 MySQL 查询后,响应是一个大约 100 个字符的字符串。所以我不认为这是一个超时的事情。
有什么想法吗?
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();
}
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在尝试了各种修复(例如 HTTP 协议更改、Expect100contine、keepalive 和一些其他请求参数)后,我不得不切换到非 SSL,并仅通过此连接加密密码。
这不是最理想的解决方案,但必须完成。某些 PC 上的某些内容似乎会阻止我们的应用程序的 SSL 连接。这是通过使用我们软件内置的浏览器视图来尝试在安全服务器上加载图像进行测试的 - 但失败了。
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?