我如何无限期地等待C#中的WebRequest的答案

发布于 2025-01-23 22:30:15 字数 1023 浏览 3 评论 0原文

我正在将网络重新发布到一个响应非常缓慢的页面上。响应的大小和持续时间各不相同。有时需要超过5分钟才能完成答复。浏览器成功地等待了答案,但是我的WebRequest事先以错误的504打破了答案。 我该如何防止这种情况。 在这里,我的代码(我将所有超时都设置为-1,没有任何成功)

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(req_url));
                request.Accept = "text/html";
                request.UserAgent = "Mozilla / 4.0(compatible; MSIE 6.0; " + "Windows NT 5.2; .NET CLR 1.0.3705;)";
                request.Timeout = -1;
                request.KeepAlive = true;
                request.ReadWriteTimeout = -1;
                request.AllowReadStreamBuffering = false;
                request.AllowWriteStreamBuffering = false;
                request.ReadWriteTimeout = -1;
                request.ContinueTimeout = -1;

,这里是一个示例查询:

http ://www.bml3.nrw.de/service/bml? 475 %2C364538.615320737%2C5704402.64151475,EPSG:25832

I'm making a webrequest to a page that is responding very slowly. the size and duration of the response varies. sometimes it takes more than 5 minutes to finish reply. the browser waits successfully for the answer, but my webrequest breaks off with error 504 beforehand.
How can I prevent this.
here my code (I set all timeouts to -1, without any success)

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(req_url));
                request.Accept = "text/html";
                request.UserAgent = "Mozilla / 4.0(compatible; MSIE 6.0; " + "Windows NT 5.2; .NET CLR 1.0.3705;)";
                request.Timeout = -1;
                request.KeepAlive = true;
                request.ReadWriteTimeout = -1;
                request.AllowReadStreamBuffering = false;
                request.AllowWriteStreamBuffering = false;
                request.ReadWriteTimeout = -1;
                request.ContinueTimeout = -1;

and here is an example query:

http://www.bml3.nrw.de/service/bml?&REQUEST=GetFeature&SERVICE=WFS&VERSION=2.0.0&srs=EPSG:25832&TYPENAMES=bml:Borehole&BBox=359538.615320737%2C5699402.64151475%2C364538.615320737%2C5704402.64151475,EPSG:25832

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

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

发布评论

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

评论(1

今天小雨转甜 2025-01-30 22:30:15

您可以在web.config

&lt; httpruntime executionTime ut =“ 1000” /&gt; < /code>

web.config:

<configuration>
    <system.web>
        <httpRuntime executionTimeout="300" />
    </system.web>
</configuration>

httpwebrequest.getResponse( )是一次性的,应用used()关闭来包装。这可能是您的根本问题90%。

using (var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse())
{
}

You can specify the request timeout (in seconds) in the web.config
<httpRuntime executionTimeout="1000" />

web.config:

<configuration>
    <system.web>
        <httpRuntime executionTimeout="300" />
    </system.web>
</configuration>

httpWebRequest.GetResponse() is disposable and should be wrapped with using() closures. This is 90% likely to be your underlying problem.

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