使用 HttpWebRequest 时定期超时

发布于 2024-10-07 08:40:45 字数 686 浏览 7 评论 0原文

我有一些发送简单 xml Web 请求的代码。它是从 Windows 服务调用的。有时,服务开始引发异常(System.Net.WebException:操作已超时),重新启动服务即可修复该问题。这是代码:

    public bool PerformXmlRequest(string xml)
    {
        var httpRequest = (HttpWebRequest)WebRequest.Create(_url);

        httpRequest.Method = "POST";

        httpRequest.ContentType = "text/xml";

        using (var xmlWriter = new StreamWriter(httpRequest.GetRequestStream(), Encoding.UTF8))
        {
            xmlWriter.WriteLine(xml);
        }

        using (var httpResponse = (HttpWebResponse)httpRequest.GetResponse())
        {
            return httpResponse.StatusDescription == "OK";
        }
    }

是否有任何明显的错误可能导致此问题?

I have some code that send a simple xml web request. It is called from a windows service. Sometimes the service starts throwing exceptions (System.Net.WebException: The operation has timed out) and a restart of the service fixes the issue. Here is the code:

    public bool PerformXmlRequest(string xml)
    {
        var httpRequest = (HttpWebRequest)WebRequest.Create(_url);

        httpRequest.Method = "POST";

        httpRequest.ContentType = "text/xml";

        using (var xmlWriter = new StreamWriter(httpRequest.GetRequestStream(), Encoding.UTF8))
        {
            xmlWriter.WriteLine(xml);
        }

        using (var httpResponse = (HttpWebResponse)httpRequest.GetResponse())
        {
            return httpResponse.StatusDescription == "OK";
        }
    }

Is there anything obviously wrong with it that might be causing this issue?

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

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

发布评论

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

评论(1

司马昭之心 2024-10-14 08:40:45

我发现调用代码没有任何问题。

错误是由客户端代码生成的还是来自服务?

如果它来自服务,则需要修复该服务,理想情况下,无论您发送什么内容,服务都不应超时,它应该以更受控的方式失败,并给出更好的错误消息。

There is nothing I can find is wrong with the calling code.

Is the error generated by client side code or does it come from the service?

If it is from the service it's the service that needs to be fixed, idéaly the service should never timeout no matter what you send it, it should fail in a more controlled maner giving a beter error message.

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