.NET 中的 WebRequest 异常

发布于 2024-08-23 13:21:38 字数 1020 浏览 6 评论 0原文

我使用此代码片段来验证 URL 中指定的文件是否存在,并每隔几秒为每个用户尝试一次。有时(主要是当有大量用户使用该网站时)代码不起作用。

    [WebMethod()]
    public static string GetStatus(string URL)
    {
        bool completed = false;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            try
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    completed = true;
                }
            }
            catch (Exception)
            {
                //Just don't do anything. Retry after few seconds
            }
        }

        return completed.ToString();
    }

当我查看 Windows 事件日志时,发现了几个错误:

Unable to read data from the transport connection. An existing connection was forcibly closed

The Operation has timed out

The remote host closed the connection. The error code is 0x800703E3

当我重新启动 IIS 时,一切正常,直到下次发生这种情况。

I use this code snippet that verifies if the file specified in the URL exists and keep trying it every few seconds for every user. Sometimes (mostly when there are large number of users using the site) the code doesn't work.

    [WebMethod()]
    public static string GetStatus(string URL)
    {
        bool completed = false;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            try
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    completed = true;
                }
            }
            catch (Exception)
            {
                //Just don't do anything. Retry after few seconds
            }
        }

        return completed.ToString();
    }

When I look at the Windows Event logs there are several errors:

Unable to read data from the transport connection. An existing connection was forcibly closed

The Operation has timed out

The remote host closed the connection. The error code is 0x800703E3

When I restart the IIS, things work fine until the next time this happens.

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

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

发布评论

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

评论(1

寒江雪… 2024-08-30 13:21:38

您将 try/catch 放入 using 语句中,而 request.GetResponse 方法可能会抛出:

bool completed = false;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
try
{
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        if (response.StatusCode == HttpStatusCode.OK)
        {
            completed = true;
        }
    }
}
catch (Exception)
{
    //Just don't do anything. Retry after few seconds
}
return completed.ToString();

You are putting the try/catch inside the using statement while it's the request.GetResponse method that might throw:

bool completed = false;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
try
{
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        if (response.StatusCode == HttpStatusCode.OK)
        {
            completed = true;
        }
    }
}
catch (Exception)
{
    //Just don't do anything. Retry after few seconds
}
return completed.ToString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文