【问题标题】:"The underlying connection was closed: An unexpected error occurred on a send." Error in WebClient.OpenRead(string link)“底层连接已关闭:发送时发生意外错误。” WebClient.OpenRead 中的错误(字符串链接)
【发布时间】:2016-07-01 04:45:45
【问题描述】:

我面临错误“底层连接已关闭:发送时发生意外错误。”在获取google API的返回数据。
在本地很好,但我已经发布并上传到服务器,我遇到了那个错误。请帮帮我.. 最好的问候...

 WebClient webClient = new WebClient();
 Stream stream = webClient.OpenRead("https://www.googleapis.com/oauth2/v1/userinfo?access_token=[Access_Token]);
 string b;

/*I have not used any JSON parser because I do not want to use any extra dll/3rd party dll*/
 using (StreamReader br = new StreamReader(stream))
      {
         b = br.ReadToEnd();
      }

【问题讨论】:

    标签: c# asp.net google-api


    【解决方案1】:

    您的连接正在连接到 HTTPS 端点,它可能需要 TLS 1.2,这可能不是您客户端的默认 TLS 版本;这是对旧版本 .NET 的修复:

    ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
    

    【讨论】:

    • 成功了!就我而言,使用 (SecurityProtocolType)768;
    【解决方案2】:

    当客户端计算机无法发送 HTTP 请求时会出现此问题。客户端计算机无法发送 HTTP 请求,因为连接已关闭或不可用。当客户端计算机发送大量数据时,可能会出现此问题。

    访问https://support.microsoft.com/en-us/kb/915599

    查看解决方案 A、D、E、F 和 O 可能会对您有所帮助。

    更新 1:

    很遗憾,以上链接已损坏。请阅读以下内容。

    要在 .Net Framework 4.0 中适应 TLS1.2,请尝试以下操作:

    ServicePointManager.SecurityProtocol = DirectCast(3072, SecurityProtocolType)
    

    如果 TLS 1.2 不适合您,请尝试以下方法:

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

    以下是有关 TLS 和 .NET 支持的一些信息:

    1. .NET 4.6 及更高版本。 您无需执行任何额外工作即可 支持TLS 1.2,默认支持。

    2. .NET 4.5. 支持 TLS 1.2,但它不是默认协议。您需要选择加入才能使用 它。以下代码将使 TLS 1.2 默认,确保执行 在连接到安全资源之前: ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12

    3. .NET 4.0. 不支持 TLS 1.2,但如果您在系统上安装了 .NET 4.5(或更高版本),那么您仍然可以选择使用 TLS 1.2,即使 您的应用程序框架不支持它。唯一的问题是 .NET 4.0 中的 SecurityProtocolType 没有条目 TLS1.2,所以我们必须使用这个枚举的数字表示 值:ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

    4. .NET 3.5 或更低版本。 不支持 TLS 1.2 (*) 并且没有解决方法。将您的应用程序升级到 框架的更新版本。

    有关 TLS 和 .NET 支持的更多信息,请访问以下链接:

    TLS 1.2 and .NET Support: How to Avoid Connection Errors

    【讨论】:

    • 那篇 kb 文章是一个断开的链接,我找不到它被移动到的位置...
    猜你喜欢
    • 1970-01-01
    • 2020-12-02
    • 2015-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多