去年,2019年,我在IIS (https,TLS1.2)上部署了一个WCF web服务,客户端代码实现了SecurityProtocolType.Tls12
,它成功地连接到web服务URL,并开始完美地工作。
今年我遇到了一个问题
基础连接已关闭。发送时发生意外错误。
我检查了web服务URL SSL证书,如果它仍然在TLS1.2上,那么它就是。
用于测试连接的客户端代码如下所示:
public bool TestConnection(string url)
{
try
{
var myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Timeout = Timeout.Infinite;
myRequest.ReadWriteTimeout = 30000;
myRequest.KeepAlive = true;
myRequest.ServicePoint.Expect100Continue = false;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
myRequest.ServerCertificateValidationCallback = (s, cert, chain, ssl) => true;
var response = (HttpWebResponse)myRequest.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
return true;
}
else
{
//well, at least it returned...
return false;
}
}
catch (Exception ex)
{
return false;
}
}
发布于 2020-02-28 09:39:01
我认为最可能的原因是,如果WCF服务和客户端以前正常工作,证书就过期了。
是否使用自签名证书进行传输层安全?另外,请尽量不要指定用于通信的TLS版本。
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
在再次调用远程服务之前,我建议您使用浏览器导航到服务URL,并检查证书错误。浏览器显示的错误消息将帮助您解决导致错误的原因。
如果问题还存在,请随时告诉我。
https://stackoverflow.com/questions/60436095
复制相似问题