【问题标题】:C# webclient - Received an unexpected EOF or 0 bytes from the transport streamC# webclient - 从传输流接收到意外的 EOF 或 0 字节
【发布时间】:2018-12-27 12:59:41
【问题描述】:

当我尝试使用 WebClient 连接到服务器时,有时会遇到此错误。

但是我已经完成了之前的问题和答案,例如Getting EOF exception over https call 关于这个。我已经尝试过他们的解决方案,但这对我没有帮助。

根据之前的问题,我还将 .net 框架从 4.5 更新到 4.6.1,但仍然面临同样的问题。

我的代码如下所示

    private static string GetWebContent(string url)
    {
        string response = null;
        try
        {
            ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;                
            using (var request = new GZipWebClient())
            {
                response = request.DownloadString(url);
                request.Dispose();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return response;
    }

【问题讨论】:

  • 可能是的。由于缺乏声誉,我无法评论这个问题。我已经尝试过该解决方案,但这并没有解决我的问题。

标签: c# .net webclient


【解决方案1】:

您的代码以某种方式尝试使用 TLS,但您发送请求的服务器不支持它。

您可以尝试明确设置SSL3

更改:

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

收件人:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

【讨论】:

  • 感谢您的回答。我已经尝试过了,但仍然会导致有关 TLS 的问题,例如“请求被中止:无法创建 SSL/TLS 安全通道。”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-27
  • 2022-12-16
  • 1970-01-01
  • 2016-06-07
  • 1970-01-01
相关资源
最近更新 更多