System.Net.WebException:操作已超时

Posted

技术标签:

【中文标题】System.Net.WebException:操作已超时【英文标题】:System.Net.WebException: The operation has timed out 【发布时间】:2015-09-13 00:16:23 【问题描述】:

我有一个复杂的代码,在一个部分中,我向 Web 服务发送了一个 rest webrequest 并获得它对剩余工作的响应,响应有不同的大小,有时它很重,在 VS 上的 IIS Express 上没有任何问题,但在 IIS 上运行时会抛出 。这是我使用的代码:

  Dim req As HttpWebRequest = TryCast(WebRequest.Create("http://www.somedomain.com/service.svc"), HttpWebRequest)
        Using TryCast(req, IDisposable)
            req.Proxy = Nothing
            req.ServicePoint.ConnectionLimit = 100
            req.ServicePoint.MaxIdleTime = 3000000
            req.Method = "POST"
            req.Headers("Accept-Encoding") = "gzip,deflate"
            req.ContentType = "application/soap+xml; charset=UTF-8"

            If compressionRequested = True Then
                ''  if you want to receive compressed response 

                req.Headers.Add("Accept-Encoding", "gzip, deflate")
            End If
            Dim stOut As New StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII)

            stOut.Write(xml.ToString)
            stOut.Close()


            Using resp As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
                If resp.ContentEncoding = "gzip" Then


                    Dim st As Stream = resp.GetResponseStream()
                    If resp.ContentEncoding.ToLower().Contains("gzip") Then
                        st = New System.IO.Compression.GZipStream(st, System.IO.Compression.CompressionMode.Decompress)
                    ElseIf resp.ContentEncoding.ToLower().Contains("deflate") Then
                        st = New System.IO.Compression.DeflateStream(st, System.IO.Compression.CompressionMode.Decompress)
                    End If
                    Dim bfst As BufferedStream = New BufferedStream(st)
                    Using stIn As New StreamReader(bfst, Encoding.[Default])
                        respXml = stIn.ReadToEnd()


                        stIn.Close()
                    End Using

                Else
                    Using stIn As New StreamReader(resp.GetResponseStream())
                        respXml = stIn.ReadToEnd()

                        stIn.Close()
                    End Using
                End If
            End Using

        End Using

我也在 web.config 中配置了这个:

<system.net>
<connectionManagement>
        <add address="*" maxconnection="100" />
        </connectionManagement>
    <settings>
      <servicePointManager expect100Continue="true" />
    </settings>
  </system.net>

有没有建议哪里有问题以及如何解决?

【问题讨论】:

【参考方案1】:

通过在此代码之前添加GC.Collect(),一切正常,

【讨论】:

以上是关于System.Net.WebException:操作已超时的主要内容,如果未能解决你的问题,请参考以下文章

System.Net.WebException:无法解析远程名称:

System.Net.WebException:操作已超时

System.Net.WebException HTTP 状态码

System.Net.WebException:请求被中止:请求被取消

ArangoDB-NET 错误:System.Net.WebException:'不知道这样的主机 不知道这样的主机'

System.Net.WebException 的 Powershell 处理