【问题标题】:The underlying connection was closed error底层连接已关闭错误
【发布时间】:2011-04-10 03:33:13
【问题描述】:

有谁熟悉这个问题,我似乎不时在我的网络服务客户端中得到它:

底层连接已关闭:接收时发生意外错误。无法从传输连接读取数据:现有连接被远程主机强行关闭。在 System.Net.HttpWebRequest.GetRequestStream(TransportContext& 上下文) 在 System.Net.HttpWebRequest.GetRequestStream()

这是我用于请求和响应网络服务的代码:

 public string GetWebResponse(string data, IOffer offer)
    {
        _loggingManager.LogProcess(offer, true, "Request", "Request", data, offer.OperatorCode, true);

        DateTime start = DateTime.Now;
        StringBuilder returnedXml = new StringBuilder();
        string outputstring = string.Empty;
        ASCIIEncoding encoding = new ASCIIEncoding();
        StreamReader r = null;

        /*try
        {*/
            if (!string.IsNullOrEmpty(data))
                outputstring = Regex.Replace(data, "\\s+", " ");

            outputstring = outputstring.Replace("utf-16", "utf-8");
            string PostData = "xml=" + outputstring;

            byte[] Bytedata = encoding.GetBytes(PostData);

            //fixme
            objWebHTTPReq = (HttpWebRequest)WebRequest.Create(GetEndpointAddress(offer));

            objWebHTTPReq.ContentType = "application/x-www-form-urlencoded";
            objWebHTTPReq.Accept = "text/html";
            objWebHTTPReq.ContentLength = Bytedata.Length;
            objWebHTTPReq.Method = "POST";

            objWebHTTPReq.KeepAlive = false;

            string httpOutput = objWebHTTPReq.Headers.ToString();

            objStream = objWebHTTPReq.GetRequestStream();
            objStream.Write(Bytedata, 0, Bytedata.Length);
            objStream.Flush();
            objStream.Close();

            WebResponse resp = objWebHTTPReq.GetResponse();
            r = new StreamReader(resp.GetResponseStream());

            returnedXml.Append(r.ReadToEnd().Replace("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "").Replace("<?xml version=\"1.0\" encoding=\"utf-8\" ?>", "").ToString());

            _loggingManager.LogProcess(offer, true, "Response", "Response", returnedXml.ToString(), offer.OperatorCode, true);

            return returnedXml.ToString();

        /*}
        catch (Exception ex)
        {
            return null;
            // fixme
            //return this.CreateErrorResponse("Handled exception:\n" + ex.ToString());
        }
        finally
        {
            if (!string.IsNullOrEmpty(outputstring))
                outputstring.Remove(0);
            if (returnedXml != null)
                returnedXml.Remove(0, returnedXml.Length);
            if (r != null)
                r.Dispose();
        }*/
    }         

【问题讨论】:

  • 这对我来说听起来像是一个超时异常

标签: c# .net web-services


【解决方案1】:

尝试为您的网络服务调用设置.Timeout

yourWebService.Timeout = -1;// never timeout.

您也可以尝试在 web.config 中设置 executionTimeout

<configuration>
  <system.web>
  <httpRuntime maxRequestLength="4000"
    executionTimeout="45"
  </system.web>
</configuration>

【讨论】:

  • 我会试试这个,看看它是否有效 - 重新创建它有点困难,因为它安装在服务器上并且只是偶尔发生!
【解决方案2】:

该信息有时颇具误导性。当没有到远程主机的路由时,您可以获得相同的消息。

当您收到错误消息时,请在同一端口上尝试 telnet。如果这可行,如果您无法远程登录,则似乎是超时,那么这是一个连接问题(没有到主机的路由)

【讨论】:

    猜你喜欢
    • 2014-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 2013-10-31
    • 2018-01-31
    • 2017-09-21
    • 2017-09-29
    相关资源
    最近更新 更多