c# - HttpWebRequest 返回错误“底层连接已关闭”
问题描述
我只需要调用一个网页并返回响应页面。我使用以下代码来调用网页。这段代码几个月来一直运行良好。但是,就在最近,调用 GetResponse 的行现在返回错误“底层连接已关闭”。我附上了 System.Net 的详细日志,但我不知道出了什么问题。如果我将 URL 更改为http://microsoft.com,那么响应就可以正常工作。它似乎与我正在调用的特定站点有关。我尝试了许多不同的对 gocomics.com 的 url 调用(如 https、没有 www 等),但都没有成功。当我查看日志时,我无法确定 GetResponse 失败的原因。
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://www.gocomics.com"));
// Set some reasonable limits on resources used by this request
request.MaximumAutomaticRedirections = 4;
request.MaximumResponseHeadersLength = 4;
// Set credentials to use for this request.
request.Credentials = CredentialCache.DefaultCredentials;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
我也尝试过使用这个简单的代码,但仍然没有成功
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://www.gocomics.com"));
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
这是在 Verbose 设置的 System.Net 日志记录
System.Net Verbose: 0 : [51100] Entering
WebRequest::Create(http://www.gocomics.com/)
System.Net Verbose: 0 : [51100] Entering HttpWebRequest#54986669::HttpWebRequest(http://www.gocomics.com/#1880366986)
System.Net Information: 0 : [51100] Current OS installation type is 'Client'.
System.Net Information: 0 : [51100] RAS supported: True
System.Net Verbose: 0 : [51100] Exiting HttpWebRequest#54986669::HttpWebRequest()
System.Net Verbose: 0 : [51100] Exiting WebRequest::Create() -> HttpWebRequest#54986669
System.Net Verbose: 0 : [51100] Entering HttpWebRequest#54986669::GetResponse()
System.Net Error: 0 : [51100] Can't retrieve proxy settings for Uri 'http://www.gocomics.com/'. Error code: 12180.
System.Net Verbose: 0 : [51100] Entering ServicePoint#23686174::ServicePoint(www.gocomics.com:80)
System.Net Information: 0 : [51100] Associating HttpWebRequest#54986669 with ServicePoint#23686174
System.Net Information: 0 : [51100] Associating Connection#31609076 with HttpWebRequest#54986669
System.Net Information: 0 : [51100] Connection#31609076 - Created connection from 10.0.0.208:13441 to 66.6.101.183:80.
System.Net Information: 0 : [51100] Associating HttpWebRequest#54986669 with ConnectStream#20903718
System.Net Information: 0 : [51100] HttpWebRequest#54986669 - Request: GET / HTTP/1.1
System.Net Information: 0 : [51100] ConnectStream#20903718 - Sending headers
{
Host: www.gocomics.com
Connection: Keep-Alive
}.
System.Net Information: 0 : [51100] Connection#31609076 - Received status line: Version=1.1, StatusCode=301, StatusDescription=Moved Permanently.
System.Net Information: 0 : [51100] Connection#31609076 - Received headers
{
Connection: Close
Content-Length: 0
Content-Type: text/html
Location: https://www.gocomics.com/
}.
System.Net Information: 0 : [51100] ConnectStream#51746094::ConnectStream(Buffered 0 bytes.)
System.Net Information: 0 : [51100] Associating HttpWebRequest#54986669 with ConnectStream#51746094
System.Net Information: 0 : [51100] Associating HttpWebRequest#54986669 with HttpWebResponse#41215084
System.Net Warning: 0 : [51100] HttpWebRequest#54986669::() - Error code 301 was received from server response.
System.Net Warning: 0 : [51100] HttpWebRequest#54986669::() - Resubmitting request.
System.Net Verbose: 0 : [51100] Entering ServicePoint#12096874::ServicePoint(www.gocomics.com:443)
System.Net Information: 0 : [51100] Associating HttpWebRequest#54986669 with ServicePoint#12096874
System.Net Information: 0 : [51100] Associating Connection#26209817 with HttpWebRequest#54986669
System.Net Information: 0 : [51100] Connection#26209817 - Created connection from 10.0.0.208:13443 to 66.6.101.183:443.
System.Net Information: 0 : [51100] TlsStream#860689::.ctor(host=www.gocomics.com, #certs=0, checkCertificateRevocationList=False, sslProtocols=Default)
System.Net Information: 0 : [51100] Associating HttpWebRequest#54986669 with ConnectStream#35288276
System.Net Information: 0 : [51100] HttpWebRequest#54986669 - Request: GET / HTTP/1.1
System.Net Information: 0 : [51100] ConnectStream#35288276 - Sending headers
{
Host: www.gocomics.com
Connection: Keep-Alive
}.
System.Net Information: 0 : [51100] SecureChannel#37533195::.ctor(hostname=www.gocomics.com, #clientCertificates=0, encryptionPolicy=RequireEncryption)
System.Net Information: 0 : [51100] Enumerating security packages:
System.Net Information: 0 : [51100] Negotiate
System.Net Information: 0 : [51100] NegoExtender
System.Net Information: 0 : [51100] Kerberos
System.Net Information: 0 : [51100] NTLM
System.Net Information: 0 : [51100] TSSSP
System.Net Information: 0 : [51100] pku2u
System.Net Information: 0 : [51100] WDigest
System.Net Information: 0 : [51100] Schannel
System.Net Information: 0 : [51100] Microsoft Unified Security Protocol Provider
System.Net Information: 0 : [51100] Default TLS SSP
System.Net Information: 0 : [51100] CREDSSP
System.Net Information: 0 : [51100] SecureChannel#37533195 - Left with 0 client certificates to choose from.
System.Net Information: 0 : [51100] SecureChannel#37533195::.AcquireClientCredentials, new SecureCredential() (flags=(ValidateManual, NoDefaultCred, SendAuxRecord), m_ProtocolFlags=(Ssl3Client, Tls10Client), m_EncryptionPolicy=RequireEncryption)
System.Net Information: 0 : [51100] AcquireCredentialsHandle(package = Microsoft Unified Security Protocol Provider, intent = Outbound, scc = System.Net.SecureCredential)
System.Net Information: 0 : [51100] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = (null), targetName = www.gocomics.com, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation)
System.Net Information: 0 : [51100] InitializeSecurityContext(In-Buffer length=0, Out-Buffer length=120, returned code=ContinueNeeded).
System.Net Error: 0 : [51100] Exception in HttpWebRequest#54986669:: - The underlying connection was closed: An unexpected error occurred on a send..
System.Net Information: 0 : [51100] Associating HttpWebRequest#54986669 with ServicePoint#12096874
System.Net Information: 0 : [51100] Associating Connection#62465990 with HttpWebRequest#54986669
System.Net Information: 0 : [51100] Connection#62465990 - Created connection from 10.0.0.208:13444 to 66.6.101.183:443.
System.Net Information: 0 : [51100] TlsStream#10968766::.ctor(host=www.gocomics.com, #certs=0, checkCertificateRevocationList=False, sslProtocols=Default)
System.Net Information: 0 : [51100] Associating HttpWebRequest#54986669 with ConnectStream#47066262
System.Net Information: 0 : [51100] HttpWebRequest#54986669 - Request: GET / HTTP/1.1
System.Net Information: 0 : [51100] ConnectStream#47066262 - Sending headers
{
Host: www.gocomics.com
Connection: Keep-Alive
}.
System.Net Information: 0 : [51100] SecureChannel#50668565::.ctor(hostname=www.gocomics.com, #clientCertificates=0, encryptionPolicy=RequireEncryption)
System.Net Information: 0 : [51100] SecureChannel#50668565 - Left with 0 client certificates to choose from.
System.Net Information: 0 : [51100] Using the cached credential handle.
System.Net Information: 0 : [51100] InitializeSecurityContext(credential = System.Net.SafeFreeCredential_SECURITY, context = (null), targetName = www.gocomics.com, inFlags = ReplayDetect, SequenceDetect, Confidentiality, AllocateMemory, InitManualCredValidation)
System.Net Information: 0 : [51100] InitializeSecurityContext(In-Buffer length=0, Out-Buffer length=120, returned code=ContinueNeeded).
System.Net Error: 0 : [51100] Exception in HttpWebRequest#54986669:: - The underlying connection was closed: An unexpected error occurred on a send..
System.Net Error: 0 : [51100] Exception in HttpWebRequest#54986669::GetResponse - The underlying connection was closed: An unexpected error occurred on a send..
解决方案
将以下内容添加到您的注册表将强制执行 TLS1.2
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft.NETFramework\v4.0.30319] "SchUseStrongCrypto"=dword:00000001
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft.NETFramework\v4.0.30319] "SchUseStrongCrypto"=dword:00000001
HKEY_LOCAL_MACHINE\SOFTWARE[Wow6432Node]Microsoft.NETFramework\: SchUseStrongCrypto 注册表项具有 DWORD 类型的值。值为 1 会导致您的应用使用强加密。强加密使用更安全的网络协议(TLS 1.2、TLS 1.1 和 TLS 1.0)并阻止不安全的协议。值 0 禁用强加密。
来源:https ://docs.microsoft.com/en-us/dotnet/framework/network-programming/tls#schusestrongcrypto
推荐阅读
- c# - Microsoft 图形授权代码流 - 以编程方式从 Web 应用程序获取授权代码
- angular - 访问共享模块中的组件功能
- javascript - 对象解构与扩展运算符配对会在内存中创建新引用吗?
- azure-sql-data-warehouse - 重命名 Azure SQL Server 中所有对象中的表
- jquery - jQuery 函数没有被按钮的类触发
- ios - 如何在异步调用 SwfitUI 后加载初始屏幕
- python - Raspberry Pi 和 ESP32 通过蓝牙进行通信
- uwp - UWP Identity Publisher 中不支持的 ST 标记
- c - _start 是否会调用我的程序的主要功能和其他必要的设置功能?
- pytorch - Imagenet_1k 数据集是否足以训练 Resnet50 模型?