..基础连接已关闭:接收时发生意外错误 [英] ..The underlying connection was closed: An unexpected error occurred on a receive

查看:1893
本文介绍了..基础连接已关闭:接收时发生意外错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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'
类型的未处理异常

error: An unhandled exception of type 'System.Net.WebException' occurred in System.dll

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

Additional information: The underlying connection was closed: An unexpected error occurred on a receive.

第<$行c $ c> HttpWebResponse myResponse =(HttpWebResponse)myRequest.GetResponse();

请帮助我。

推荐答案

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


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

This problem occurs when the server or another network device unexpectedly closes an existing Transmission Control Protocol (TCP) connection. This problem may occur when a time-out value on the server or on the network device is set too low. To resolve this problem, see resolutions A, D, E, F, and O. The problem can also occur if the server resets the connection unexpectedly, such as if an unhandled exception crashes the server process. Analyze the server logs to see if this may be the issue.

分辨率

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

To resolve this problem, make sure that you are using the most recent version of the .NET Framework.

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

Add a method to the class to override the GetWebRequest method. This change lets you access the HttpWebRequest object. If you are using Microsoft Visual C#, the new method must be similar to the following.

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请求时,会收到一个或多个错误消息。 a>。

Excerpt from KB915599: You receive one or more error messages when you try to make an HTTP request in an application that is built on the .NET Framework 1.1 Service Pack 1.

这篇关于..基础连接已关闭:接收时发生意外错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆