即使设置了无限超时,Webrequest 也会超时

Webrequest timed out even with infinite timeout set

我有一个使用外部节点模块打开 HttpWebRequest 的 C# 方法。

然后执行一些长数据库查询,处理这些数据并将它们发送到节点模块。

节点模块通过套接字接收来自 c# 的数据,并将该数据转换为将返回到 c# 的其他数据。

对于一些需要大约 30 分钟的过程的测试,一切顺利。但是一个更大的测试需要大约 2 小时,我有 the request was aborted the operation has timed out

这是我的部分代码:

HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.ContentType = "application/json";
request.Method = "POST";
request.Timeout = -1;
request.KeepAlive = false; // both true & false values were tested, but gave the same result
request.ServicePoint.ConnectionLeaseTimeout = -1;
request.ServicePoint.MaxIdleTime = -1;
Stream st = request.GetRequestStream();
StreamWriter stw = new StreamWriter(st);
stw.Write("");

//Long process of data, queries executions and writing in stw that is received by the node module

//this line is where it throw the exception of timeout, and here is where I'm supposed to get the output from the node module, based on what I've sent
Stream stmResponse = request.GetResponse().GetResponseStream();

不应断开连接,因为它是从 C# 持续发送数据,由节点模块立即处理。

我还在 web.config

中更改了超时配置
  <system.web>
    <httpRuntime executionTimeout="180000" /> ...

服务器是否需要 2 小时才能响应?

如果没有,那你为什么不立即得到响应然后继续你的处理而不是让流打开 2 小时?

无论如何,在这种情况下,您已将 客户端 的超时设置为无限,但是,服务器可能会由于缺少 [=23] =] header.

试试这个:

request.KeepAlive = true;

我找出了问题的根源,这与代码根本无关,因为与外部模块的连接是通过 VPN 和防火墙完成的,并且两者都切断了连接在某个时间。

删除 VPN 和防火墙后,不再抛出超时异常。