使用 HttpWebRequest 时定期超时
我有一些发送简单 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现调用代码没有任何问题。
错误是由客户端代码生成的还是来自服务?
如果它来自服务,则需要修复该服务,理想情况下,无论您发送什么内容,服务都不应超时,它应该以更受控的方式失败,并给出更好的错误消息。
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.