【问题标题】:Error "The underlying connection was closed: An unexpected error occurred on a send" on remote server远程服务器上的错误“底层连接已关闭:发送时发生意外错误”
【发布时间】:2019-02-19 07:04:37
【问题描述】:

我有一个相当简单的代码,直到最近才有效,直到它发送数据的站点没有切换到 https。 在调试模式下(在本地主机上),我使用通过域登录名和密码访问的代理。在发布模式下,我使用另一个不需要凭据的代理。 我尝试添加

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

它没有用!而是只有一半。如果凭据以调试模式发送,则发送请求,但除了“底层连接已关闭:如果您使用 DefaultCredentials。您能给我什么建议?

#if DEBUG //OK
        NetworkCredential proxyCreds = new NetworkCredential(
            "login",
            "password"
        );

        WebProxy proxy = new WebProxy("http://bproxy.com:3131", false)
        {
            UseDefaultCredentials = false,
            Credentials = proxyCreds,
        };

        HttpClient client = null;
        HttpClientHandler httpClientHandler = new HttpClientHandler()
        {
            Proxy = proxy,
            PreAuthenticate = true,
            UseDefaultCredentials = false,
        };

        client = new HttpClient(httpClientHandler);
#else   // NOT OK
        HttpClientHandler httpClientHandler = new HttpClientHandler()
        {
            Proxy = new WebProxy(new Uri("http://bproxy.com:3128"))
        };

        HttpClient client = new HttpClient(httpClientHandler);
#endif

        client.BaseAddress = new Uri("http://url.com");
        ServicePointManager.SecurityProtocol = 
            SecurityProtocolType.Tls12 | 
            SecurityProtocolType.Tls11 | 
            SecurityProtocolType.Tls;

        var values = new Dictionary<string, string>
        {
           {"_method", "POST" },
           { "data[User][email]", user },
           { "data[User][password]", pass }
        };

        var content = new FormUrlEncodedContent(values);

        var responseString = Post(client, "/", content);

【问题讨论】:

  • 这是网络错误。当然,每个环境的网络情况都不同。您将需要进行一些基本的根本原因分析。检查服务器日志以查看是否收到任何请求。使用浏览器尝试相同的请求。等等。它很容易与您的代码无关。

标签: c# ssl


【解决方案1】:

我认为您需要将您在托管它的网络上使用的那些代理地址列入白名单。将 IP 或代理地址(包括端口)列入白名单

与您的网络团队沟通。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-02
    • 2015-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多