..基础连接已关闭:接收时发生意外错误

我有以下代码:

private Uri currentUri; private void Form1_Load(object sender, EventArgs e) { currentUri = new Uri(@"http://www.stackoverflow.com"); HttpWebRequest myRequest = (HttpWebRequest) HttpWebRequest.Create("http://www.stackoverflow.com"); WebProxy myProxy = new WebProxy("120.198.230.8:81"); myRequest.Proxy = myProxy; HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); webBrowser1.DocumentStream = myResponse.GetResponseStream(); webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating); } void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e) { if (e.Url.AbsolutePath != "blank") { currentUri = new Uri(currentUri, e.Url.AbsolutePath); HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(currentUri); HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); webBrowser1.DocumentStream = myResponse.GetResponseStream(); e.Cancel = true; } } 

编译后:

错误:System.dll中出现类型为“System.Net.WebException”的未处理exception

附加信息:基础连接已关闭:接收时发生意外错误。

在线HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

请帮我

底层连接已关闭:接收时发生意外错误。

当服务器或其他网络设备意外关闭现有传输控制协议(TCP)连接时,会发生此问题。 当服务器或网络设备上的超时值设置得太低时,可能会出现此问题。 要解决此问题,请参阅分辨率A,D,E,F和O.如果服务器意外重置连接,例如未处理的exception导致服务器进程崩溃,也会出现此问题。 分析服务器日志以查看是否存在此问题。

解析度

若要解决此问题,请确保您使用的是最新版本的.NET Framework。

向类添加方法以覆盖GetWebRequest方法。 此更改允许您访问HttpWebRequest对象。 如果您使用的是Microsoft Visual C#,则新方法必须类似于以下方法。

 class MyTestService:TestService.TestService { protected override WebRequest GetWebRequest(Uri uri) { HttpWebRequest webRequest = (HttpWebRequest) base.GetWebRequest(uri); //Setting KeepAlive to false webRequest.KeepAlive = false; return webRequest; } } 

KB915599的摘录:当您尝试在.NET Framework 1.1 Service Pack 1上构建的应用程序中发出HTTP请求时,会收到一条或多条错误消息 。

HttpWebRequest.KeepAlive设置为false对我不起作用。

由于我访问的是HTTPS页面,因此我必须将服务点安全协议设置为Tls12。

 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 

请注意,还有其他SecurityProtocolTypesSecurityProtocolType.Ssl3SecurityProtocolType.TlsSecurityProtocolType.Tls11

因此,如果Tls12不适合您,请尝试其余三个选项。

另请注意,您可以设置多个协议。 在大多数情况下这是优选的。

 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; 

没有任何解决方案适合我。 我最终发现的是以下组合:

  • 客户端系统:Windows XP Pro SP3
  • 客户端系统安装了.NET Framework 2 SP1,3,3.5
  • 使用经典Web服务(.asmx)定位.NET 2的软件
  • 服务器:IIS6
  • 网站“安全通信”设置为:
    • 需要安全通道
    • 接受客户证书

在此处输入图像描述

显然,这是造成这个问题的最后一个选择。 我通过尝试直接在Internet Explorer中打开Web服务URL来发现这一点。 它只是无限期地挂起试图加载页面。 禁用“接受客户端证书”允许页面正常加载。 我不确定这个特定系统是否存在问题(可能是一个故障的客户端证书?)因为我没有使用客户端证书,所以这个选项对我有用。