c# - 从传输流接收到意外的 EOF 或 0 字节。ServicePointManager 的设置无法修复
问题描述
当我尝试使用 WebClient 下载字符串时出现该异常。我尝试了很多案例来设置 ServicePointManager:
ServicePointManager.Expect100Continue: true and false
ServicePointManager.SecurityProtocol: SecurityProtocolType.Ssl3
和
SecurityProtocolType.Tls12
和
(SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12)
以及所有 Tls 和所有协议。每种组合都被使用了。不工作。
但是如果我使用 curl 命令 - 我会很好地获得 json。
curl -X GET https://myurl/2019-05-29
来自浏览器系统的相同行为: Windows Server 2012 R2
.NET:Framework 4.6.1
调用栈:
Failed to get fixtures from URI=https://myurl/2019-05-29. System.Net.WebException:
An exception occurred during a WebClient request. --->System.IO.IOException: Received an unexpected EOF or 0 bytes from the transport stream.
at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.WebClient.DownloadBitsState.RetrieveBytes(Int32& bytesRetrieved)
at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream, CompletionDelegate completionDelegate, AsyncOperation asyncOp)
at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
--- End of inner exception stack trace ---
at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
at System.Net.WebClient.DownloadString(Uri address)
at System.Net.WebClient.DownloadString(String address)
一种情况下的代码:
ServicePointManager.ServerCertificateValidationCallback +=
(sender, cert, chain, sslPolicyErrors) => true;
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls |
SecurityProtocolType.Tls11 |
SecurityProtocolType.Tls12 |
SecurityProtocolType.Ssl3;
using (var wc = new WebClient())
{
try
{
var json = wc.DownloadString(uri);
return json;
}
catch (Exception e)
{
return null;
}
}
解决方案
推荐阅读
- mysql - MySql 中将 ID 列加一的命令
- excel - Excel 或 Google 工作表单元格值在不同的单元格中创建
- azure-artifacts - 通过 Azure API 创建 Azure Artifacts 通用包源 - 示例
- html - 两个 div 水平居中放置在包装 div 中
- python - 两个版本的python安装在两个地方
- javascript - 如何在不提供父名称的情况下访问 firebase 中相同命名的子元素
- c++ - 从向量转换
to char* 包含垃圾数据 - csvhelper - CsvHelper 使用 ClassMap 格式化十进制
- c - 当我认为它应该在 linux 上时,c fputc 不会返回错误,在 windows 上按预期工作
- firebase - 安装 Firebase 函数的依赖项时出现错误 404