2010-09-09 137 views
1

是任何人都熟悉這個問題,我好像得到它每一個現在,然後在我的Web服務客戶端:的基礎連接已關閉錯誤

基礎連接已關閉:接收時發生意外錯誤。無法從傳輸連接讀取數據:現有連接被遠程主機強制關閉。在System.Net.HttpWebRequest.GetRequestStream(TransportContext &上下文) 在System.Net.HttpWebRequest.GetRequestStream()

這是我使用的請求和響應Web服務代碼:

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(); 
     }*/ 
    }   
+0

這聽起來像是超時異常對我 – MUG4N 2010-09-09 08:26:15

回答

2

嘗試爲您的Web服務調用設置.Timeout。你

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

也可以嘗試在你的web.config設置executionTimeout

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

我會試試這個,看看它是否工作 - 有點難以重新 - 創建它作爲它安裝在服務器上,只發生零星! – TheLearner 2010-09-09 09:26:56

2

該消息是在時間相當誤導。當沒有路由到遠程主機時,您可以獲得相同的消息。

當您收到錯誤時,嘗試在同一端口上使用telnet。如果這樣的作品,它似乎是一個超時,如果你不能telnet那麼這是一個連接問題(沒有路由主機)

相關問題