2013-03-27 185 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

相关问题