HttpWebRequest 返回错误"The underlying connection was closed"



我只需要调用一个网页并返回响应页面。我使用以下代码来调用网页。这个代码几个月来一直运行良好。然而,就在最近,调用GetResponse的行现在返回错误"基础连接已关闭"。我已经附上了System.Net的详细日志,但我不知道出了什么问题。如果我将URL更改为http://microsoft.com那么响应工作得很好。这似乎与我正在呼叫的特定网站有关。我尝试过很多对gocomics.com的不同url调用(比如https、no-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..

站点重定向到HTTPS,并且您显式设置了ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12。可能站点不支持该协议版本,正在关闭连接。

将以下内容添加到注册表将强制执行TLS1.2

[HHKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft.NETFramework\v4.0.30319]"SchUseStrongCrypto"=双字:0000000 1

[HHKEY_LOCAL_MACHINE\SOFTWARE\Microsoft.NETFramework\v4.0.30319]"SchUseStrongCrypto"=双字:0000000 1

HKEY_LOCAL_MACHINE\SOFTWARE[Wow6432Node]Microsoft.NETFramework\:SchUseStrongCrypto注册表项具有DWORD类型的值。值为1会导致您的应用程序使用强加密。强加密使用更安全的网络协议(TLS 1.2、TLS 1.1和TLS 1.0(,并阻止不安全的协议。值为0将禁用强加密。

来源:https://learn.microsoft.com/en-us/dotnet/framework/network-programming/tls#schusestrongcrypto

以下可能是解决方案-

  1. 您可以尝试使用不同或多个安全协议:

安全协议类型:安全协议类型.Ssl3,安全协议类型.Tls,安全协议类型.Tls11

  1. 由于此代码以前工作,可能也是服务器端的问题,他们禁用了对TLS1.0 的支持

  2. HttpWebRequest.KeepAlive设置为False

相关内容

  • 没有找到相关文章

最新更新