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

发布于 2024-07-17 06:43:09 字数 1211 浏览 9 评论 0原文

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

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

  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;
    }

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.

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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千寻… 2024-07-24 06:43:09

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

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();
            }
        }
    }
}

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

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.

明媚如初 2024-07-24 06:43:09

感谢您的回复,问题是由于远程服务器上的 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;
    }

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:

    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;
    }
离笑几人歌 2024-07-24 06:43:09

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

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文