【问题标题】:Timeout: The underlying connection was closed: The connection was closed unexpectedly超时:底层连接已关闭:连接意外关闭
【发布时间】:2013-04-19 04:25:38
【问题描述】:

我遇到了超时问题。我连接的服务在及时响应时运行良好。当它需要大约 10 秒时,它会超时,但有异常。除了 Timeout 和 ReadWriteTimeout,我还可以设置哪些其他超时属性以使其正常等待?我需要在 TCP/IP 级别设置什么吗?感谢您的帮助。

System.Net.WebException:底层连接已关闭:连接意外关闭。 在 System.Net.HttpWebRequest.GetResponse()

这是我的代码。我尝试了很多变化:

'The post works
Dim _httpRequest As HttpWebRequest
_httpRequest = WebRequest.Create("mywebservice")
System.Net.ServicePointManager.Expect100Continue = False
_httpRequest.Credentials = CredentialCache.DefaultCredentials
_httpRequest.ProtocolVersion = HttpVersion.Version10
'This is a 60 second wait
_httpRequest.Timeout = 60000 
'This is a 60 second wait
_httpRequest.ReadWriteTimeout = 60000
_httpRequest.KeepAlive = False
_httpRequest.Method = "POST"

'If the response takes takes 10 seconds
'This is the message we get:  System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly.

_httpRequest.Method = "GET"
_httpRequest.ContentType = "text/xml"
'The error is here
Dim httpResponse As HttpWebResponse = _httpRequest.GetResponse

【问题讨论】:

  • 您检查过 IIS 中的超时设置吗?或者你使用的任何网络服务器(你没有指定)。
  • 梅兰妮。感谢您及时的回复。它实际上是一个正在超时的本地 Web 服务。我们有一个 3rd 方服务,我们执行发布和访问。示例:HTTP://10.10.50.28:22280/transaction/identifyCustomer
  • 恐怕我对超时设置的想法就是我所知道的。希望其他人能够帮助您。祝你好运!
  • 是否有可以设置的 machine.config 设置或可以设置的 app.config 设置?即使我将超时设置为 60 秒,连接也会在 10 秒后关闭。

标签: vb.net


【解决方案1】:

您设置的Timeout 值是60000,转换为大约1 分钟,因为Timeout 值以毫秒为单位。值 1000 是 1 秒。 因此,请尝试将其适当地设置为更高的值。例如尝试将其设置为 5 或 10 分钟进行测试。

【讨论】:

  • 这没有帮助。就像超时无法识别一样。连接仅在 10 秒后关闭。
  • 如果你设置_httpRequest.KeepAlive = True会发生什么