【问题标题】:Periodic timeouts when using HttpWebRequest使用 HttpWebRequest 时的周期性超时
【发布时间】:2010-12-14 09:26:26
【问题描述】:

我有一些代码可以发送一个简单的 xml 网络请求。它是从 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";
        }
    }

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

【问题讨论】:

    标签: memory-leaks httpwebrequest timeout system.net.webexception


    【解决方案1】:

    我找不到调用代码有什么问题。

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

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

    【讨论】:

    • 你是对的。代码没有任何问题。这是一个间歇性的服务器问题。有几点意见。
    猜你喜欢
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    相关资源
    最近更新 更多