开发者

Periodic timeouts when using HttpWebRequest

开发者 https://www.devze.com 2023-01-31 00:52 出处:网络
I have some code that send a simple xml web request.开发者_如何学C It is called from a windows service. Sometimes the service starts throwing exceptions (System.Net.WebException: The operation has tim

I have some code that send a simple xml web request.开发者_如何学C 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消