请求被中止:无法创建 SSL/TLS 安全通道 c# Webrequest

The request was aborted: Could not create SSL/TLS secure channel c# Webrequest

我正在尝试使用以下 C# 代码集来调用服务。 我正在使用 .NET Framework 4.6.2。每当我到达代码 (HttpWebResponse)myHttpWebRequest.GetResponse() 时,它都会抛出异常“请求被中止:无法创建 SSL/TLS 安全通道”。我尝试了堆栈溢出中提到的许多解决方案。但是 none 对我没有帮助。从上周开始,我一直在努力解决这个问题,然后终于在这里发帖。请任何帮助将不胜感激。 我什至在当地商店添加了证书,但仍然没有用。下面是我的代码。

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

Uri myUri = new Uri("https://CallingUrl");
WebRequest myWebRequest = HttpWebRequest.Create(myUri);
HttpWebRequest myHttpWebRequest = (HttpWebRequest)myWebRequest;
string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("User:Pwd"));
myHttpWebRequest.Headers.Add("Authorization", "Basic " + svcCredentials);
myHttpWebRequest.PreAuthenticate = true;
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
myHttpWebRequest.Method = "POST"
HttpWebResponse httpResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

上面的代码完全没问题。但这个问题是由其他原因引起的。

在深入研究之后终于找到了解决方案,并认为它会对面临同样问题的其他人有所帮助,所以发布了这个答案。

原因:

Schannel 实现中的默认限制为 32768 字节。一旦达到最大限制,它将在服务之间的握手时出现 return 以上错误。

分辨率:

  1. 使用注册表项将 MessageLimitClient 从默认值 0x8000 增加到最大值 0xf000 HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\SecurityProviders\Schannel\消息

  2. 创建 Messaging 密钥(如果不存在)

  3. Then Create New DWORD name it as 'MessageLimitClient' and set the Valuedata as 'f000' (十六进制).

  4. 更改此值后,重新启动服务器以使新值生效。