基础连接已关闭:连接意外关闭
使用WebClient时收到错误消息。我用asp代码对它进行了测试,运行得很好。但是,当我尝试运行代码的exe版本时,它运行以下错误,并且我能够从服务器访问请求地址,并且它位于内部网络中。
我的服务器设置: - Windows Server 2003 Entrerprise SP2 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 :(得分: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;
}