身份验证失败,因为远程方已关闭传输流,需要解决方案

Authentication failed because remote party has closed the transport stream, need solution

我们公司的VPC使用的是http系统代理。由于事实上,没有https代理,任何网络链接都应该使用http代理,即使它是https。

我尝试将这两个解决方案添加到我的 C# 代码中,但没有帮助。

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

ServicePointManager.ServerCertificateValidationCallback =  delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
{ 
   return true; 
};

一些细节。

我也用O365图写了一个python代码apipython.

解决方法非常简单。

更改connection.py源代码
if proxy_server and proxy_port:
            if proxy_username and proxy_password:
                self.proxy = {
                    "http": "http://{}:{}@{}:{}".format(proxy_username,
                                                        proxy_password,
                                                        proxy_server,
                                                        proxy_port),
                    "https": "https://{}:{}@{}:{}".format(proxy_username,
                                                          proxy_password,
                                                          proxy_server,
                                                          proxy_port),
                }
            else:
                self.proxy = {
                    "http": "http://{}:{}".format(proxy_server, proxy_port),
                    "https": "https://{}:{}".format(proxy_server, proxy_port),
                }

if proxy_server and proxy_port:
            if proxy_username and proxy_password:
                self.proxy = {
                    "http": "http://{}:{}@{}:{}".format(proxy_username,
                                                        proxy_password,
                                                        proxy_server,
                                                        proxy_port),
                    "https": "http://{}:{}@{}:{}".format(proxy_username,
                                                          proxy_password,
                                                          proxy_server,
                                                          proxy_port),
                }
            else:
                self.proxy = {
                    "http": "http://{}:{}".format(proxy_server, proxy_port),
                    "https": "http://{}:{}".format(proxy_server, proxy_port),
                }

系统正常。

已解决。在任意位置添加这两行代码。

 Environment.SetEnvironmentVariable("HTTP_PROXY","http://your_proxy_address:port");
 Environment.SetEnvironmentVariable("HTTPS_PROXY","http://your_proxy_address:port");

在 GitHub 中搜索“dotnet 运行时”的源代码非常困难。

对于 .NET Framework 4.x:

  • 如果您的程序代码在 .NET Framework 上运行4.x...
  • ...and 如果你的程序使用 HttpClient, HttpWebRequest, 甚至可怕的 WebClient class 而没有任何进一步的诡计(例如自定义 HttpMessageHandler classes 自己进行 HTTPS 到 HTTP 的转换,或其他实现代理逻辑的尝试)
  • 然后 您可以在 app.config 中指定一个 HTTP 代理(或者 web.config 如果您的应用程序在 IIS 中运行)with the <defaultProxy> element and one-or-more child <proxy /> elements:
<configuration>

    <system.net>
        <defaultProxy enabled="true" useDefaultCredentials="false">
            <proxy autoDetect="false" bypassOnLocal="false"  proxyAddress="http://your-proxy-server:1234" useSystemDefault="false" />
        </defaultProxy>
    </system.net>

</configuration>

对于 .NET Core 3.0 或更高版本、.NET 5、.NET 6 及更高版本: