2013-03-27 182 views
0

基礎連接已關閉:連接被意外關閉System.Net.WebClient基礎連接已關閉

我使用Web客戶端時收到錯誤消息。我已經用asp代碼測試過了,它運行得很好。但是當我嘗試運行exe版本的代碼時,它運行以下錯誤,並且我能夠從服務器訪問請求地址,並且它位於內部網絡中。

我的服務器設置: - 的Windows Server 2003 SP2 Entrerprise asp.net 2.0.50727 IIS V6.0

下面是一些代碼:

手動訪問

protected static String cross_server_req(String strContent, String strPage, String strPrivateKey) 
    { 
     try 
     { 
      WebClient wc = new WebClient(); 
      String strURL = SERVICE_PATH + strPage + "?r=" + DES_.DESEncrypt(strContent); 
      byte[] responseArray = wc.DownloadData(strURL); 
      String strResData = Encoding.UTF8.GetString(responseArray); 
      if (Config.RecordWebService == "1") 
      { 
       String strLogContent = "Request:" + strContent + "\r\nResponse:" + strResData + "\r\nDateCreated:" + D_Time.DNow + "\r\n\r\n"; 
       ServiceLog.Logger(strPage, strLogContent); 
      } 
      if (null != strResData && String.Empty != strResData) 
      { 
       return strResData.Trim(); 
      } 
     } 
     catch (Exception ex) 
     { 
      ServiceLog.Logger("cross_server_req() Exception:" + ex.Message + "\r\n" + ex.StackTrace); 
     } 
     return null; 
    } 

這裏是從日誌

cross_server_req() Exception:The underlying connection was closed: The connection was closed unexpectedly. 
at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request) 
at System.Net.WebClient.DownloadData(Uri address) 
at System.Net.WebClient.DownloadData(String address) 
at Account.cross_server_request_curl(String strContent, String strPage, String strPrivateKey) 

請讓我知道任何建議,幫助這個問題?我查看了該網站,並找不到解決方案。

+0

使用wcf服務嗎? – 2013-03-27 11:29:52

+0

我沒有使用wcf服務。 – Gary 2013-03-28 04:44:21

回答

0

我相信這個錯誤可能與TLS/SSL連接有關,假設您點擊一個HTTPS網址。看看this thread。基本上你需要提供一些代碼來驗證證書是否有效。對於有指紋問題或不完全信任的CA,這是需要的。希望這有助於解決您的問題。

System.Net.ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(validateSSLCert); 


private static bool validateSSLCert(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error) 
{ 
    return true; 
} 
+0

感謝James的反饋。但是對於我們的網站,我們只使用HTTP,沒有任何TLS/SSL。 – Gary 2013-03-28 03:40:19

相關問題