我总是收到“超时”消息C# WebRequest 错误
我创建了一个从 HTTP 请求加载数据的程序。 它必须每 1 秒或 0.25 秒加载一次。
然而我发现我的许多请求(每分钟都有一些)都超时了。 这很奇怪,因为尝试在网络浏览器中重新加载相同的地址,即使经常重新加载,我总是能很快获得内容。
这是我使用的代码:
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "GET";
myRequest.Timeout = 1500;
myRequest.KeepAlive = false;
myRequest.ContentType = "text/xml";
WebResponse myResponse;
try
{
myResponse = myRequest.GetResponse();
}
catch (Exception e)
{
return e.Message;
}
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
string result = sr.ReadToEnd();
sr.Close();
myResponse.Close();
我得到的错误是:
The operation has timed out
我尝试将 keepalive 设置为 true 和 false。
那么问题来了——有什么办法可以减少超时的次数吗?我提出的每一个要求都很重要。 我很确定这与我经常查询同一地址这一事实有关,但肯定有办法阻止它......?
多谢!
I've created a program that loads data from an HTTP request.
It has to load it every 1 sec or 0.25 sec.
However I've found that that many of my request - a few every minute - are timed out.
It's weird because trying to reload the same address in a web browser, even when reloading very often, I always get the contents quickly.
This is the code I use:
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "GET";
myRequest.Timeout = 1500;
myRequest.KeepAlive = false;
myRequest.ContentType = "text/xml";
WebResponse myResponse;
try
{
myResponse = myRequest.GetResponse();
}
catch (Exception e)
{
return e.Message;
}
StreamReader sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
string result = sr.ReadToEnd();
sr.Close();
myResponse.Close();
And the error I get is:
The operation has timed out
I've tried with both keepalive set to true and false.
So the question is - is there any way to reduce the amount of timeouts? Every request I request is crucial.
I'm pretty sure it's relevant to the fact that I query the same address often, but surely there's a way to prevent it...?
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有两种超时。客户端超时和服务器超时。这里你会得到服务器超时,这可能是由于 Web 服务器的安全防御机制 (IDS/IPS) 造成的,因为它会将模式(短时间内来自单个 IP 的大量请求)识别为 DoS(拒绝服务) ) 攻击。那么你的IP就会被封禁一段时间。在这种情况下,像您在代码中所做的那样增加客户端超时是没有用的。
@Comment:关闭 KeepAlive 会使您的应用程序在每个请求上创建与 Web 服务器的新连接。也许这就是你的代码和从浏览器运行 javascript 之间的巨大区别。将 KeepAlive 设置为 true。
应用程序配置
There are two kind of timeouts. Client timeout and server timeout. Here you get server timeout which is probably due to a security defense mechanism (IDS/IPS) from web server as it recognizes the pattern (large amount of requests from a single IP in a small period of time) as a DoS (Denial of Service) attack. Then your IP is banned for a period of time. Increasing client timeout as you did in your code is not useful at this case.
@Comment: Turning off KeepAlive makes you application create new connection to web server on each request. Maybe this is the big difference between your code and running the javascript from the browser. Set KeepAlive to true.
App.Config
尝试将超时设置为Timeout.Infinite:
Try setting the timeout to Timeout.Infinite: