HttpWebRequest返回错误“基础连接已关闭”

时间:2018-11-28 22:34:44

标签: c# asp.net httpwebrequest httpwebresponse system.net.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..

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

以下可能的解决方案-

  1. 您可以尝试使用其他或多个SecurityProtocols:
  

SecurityProtocolTypes:SecurityProtocolType.Ssl3,   SecurityProtocolType.Tls,SecurityProtocolType.Tls11

  1. 由于此代码之前一直有效,因此服务器端也可能禁用了TLS1.0支持。

  2. HttpWebRequest.KeepAlive设置为False

答案 2 :(得分:0)

在注册表中添加以下内容将强制执行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