C# System.Net.WebException:底层连接已关闭:接收时发生意外错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/751056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:44:39  来源:igfitidea点击:

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive

c#webrequest

提问by

I'm trying to create a method in C# to return a string of a web pages html content from the url. I have tried several different ways, but I am getting the error System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive.

我正在尝试在 C# 中创建一个方法来从 url 返回网页 html 内容的字符串。我尝试了几种不同的方法,但我收到错误System.Net.WebException:底层连接已关闭:接收时发生意外错误。

The following works fine locally, but gets the above error when running on a remote server:

以下在本地工作正常,但在远程服务器上运行时出现上述错误:

  public static string WebPageRead(string url)
    {
        string result = String.Empty;

        WebResponse response = null;
        StreamReader reader = null;

        try
        {
            if (!String.IsNullOrEmpty(url))
            {
                HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
                request.Method = "GET";
                request.KeepAlive = false;
                request.ProtocolVersion = HttpVersion.Version10;

                response = request.GetResponse();
                reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                result = reader.ReadToEnd();
            }
        }
        catch (Exception exc)
        {
            throw exc;
        }
        finally
        {
            if (reader != null)
            {
                reader.Close();
            }

            if (response != null)
            {
                response.Close();
            }
        }

        return result;
    }

采纳答案by John Saunders

Thanks for the responses, the problem was due to a DNS issue on the remote server! Just to confirm, I went with the following code in the end:

感谢您的回复,问题是由于远程服务器上的 DNS 问题!为了确认,我最后使用了以下代码:

    public static string WebPageRead(string url)
    {
        string content = String.Empty;

        if (!String.IsNullOrEmpty(url))
        {
            HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;

            if (request != null)
            {
                request.Method = "GET";
                request.KeepAlive = false;
                request.ProtocolVersion = HttpVersion.Version10;

                try
                {
                    using (WebResponse response = request.GetResponse())
                    {
                        using (Stream stream = response.GetResponseStream())
                        {
                            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                            {
                                content = reader.ReadToEnd();
                            }
                        }
                    }
                }
                catch (Exception exc)
                {
                    throw exc;
                }
            }
        }                    

        return content;
    }

回答by Fung

Had a problem like this before that was solved by opening the url in IE on the machine with the problem. IE then asks you whether you want to add the url to the list of secure sites. Add it and it works for that url.

之前有过这样的问题,通过在有问题的机器上在 IE 中打开 url 来解决。然后 IE 会询问您是否要将 url 添加到安全站点列表中。添加它,它适用于该网址。

This is just one of the possible causes. Seriously a lot of other problems could cause this. Besides the problem described above, the best way I've found to solve this is the just catch the exception and retry the request.

这只是可能的原因之一。严重的是,许多其他问题都可能导致这种情况。除了上述问题之外,我发现解决此问题的最佳方法是捕获异常并重试请求。

回答by John Saunders

This is probably not the problem, but try the following:

这可能不是问题,但请尝试以下操作:

public static string WebPageRead(string url)
{
    if (String.IsNullOrEmpty(url))
    {
        return null;
    }

    HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
    if (request == null)
    {
        return null;
    }

    request.Method = "GET";
    request.KeepAlive = false;
    request.ProtocolVersion = HttpVersion.Version10;

    using (WebResponse response = request.GetResponse())
    {
        using (Stream stream = response.GetResponseStream())
        {
            using (StreamReader reader = 
                       new StreamReader(stream, Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }
    }
}

I echo the earlier answer that suggests you try this with a known good URL. I'll add that you should try this with a known good HTTP 1.1 URL, commenting out the line that sets the version to 1.0. If that works, then it narrows things down considerably.

我回应了较早的答案,建议您使用已知的良好 URL 尝试此操作。我将补充一点,您应该使用已知良好的 HTTP 1.1 URL 尝试此操作,注释掉将版本设置为 1.0 的行。如果这有效,那么它会大大缩小范围。