HttpWebRequest returning error "The underlying connection was closed"

httpwebrequest.getresponse exception handling
httpwebrequest getresponse 400 bad request
the remote server returned an error: (500) internal server error
httpwebrequest getresponse not returning
webexception
the remote server returned an error: (403) forbidden c# httpclient
c# httpwebrequest getresponse null
httpwebrequest getresponse sendfailure

I simply need to call a web page and get the response page back. I use the following code to call the webpage. This code has been working great for months. However, just recently, the line that calls GetResponse is now returning the error "The underlying connection was closed". I have attached the Verbose logs of the System.Net but I cannot figure out what is wrong. If I change the URL to http://microsoft.com then the response works just fine. It seems to be related to the specific site I am calling. I have tried many different url calls to gocomics.com (like https, no www, etc.) but nothing has been successful. When I look at the logs, I cannot determine why the GetResponse is failing.

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();

I have also tried just using this simple code but still not success

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("http://www.gocomics.com"));
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

Here is the System.Net logging set at Verbose

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..

The site is redirecting to HTTPS, and you explicitly set ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12. Perhaps the site does not support that protocol version and is closing the connection.

HttpWebRequest.GetResponse Method (System.Net), Returns a response from an Internet resource. HttpWebRequest.GetResponse Method. Definition -or-. An error occurred while processing the request. HttpWebRequest returning Internal Server Error (500) on IIS 6 only | The ASP.NET Forums. When requesting a web site from a local IIS 6 server, i keep receiving an Internal Server Error (500), this occures on 2 local IIS 6 servers. Using the url from the browser returnes the Web site; n

Following might the solutions -

  1. You can try to use different or multiple SecurityProtocols :

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

  1. As this code was working before, it might be the issue on server side as well where they have disabled support for TLS1.0

  2. Set HttpWebRequest.KeepAlive to False

WebException.Response Property (System.Net), Gets the response that the remote host returned. When the response to an Internet request indicates an error, WebRequest.GetResponse sets the Status  I wanted a small bit of code to grab the headers from a website, but it's breaking on me mercilessly. Here it is: public String GetWebHeaders(string url) { StringBuilder sb = new StringBuilder(); t

Adding the following to your registry will enforce 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

The HKEY_LOCAL_MACHINE\SOFTWARE[Wow6432Node]Microsoft.NETFramework\: SchUseStrongCrypto registry key has a value of type DWORD. A value of 1 causes your app to use strong cryptography. The strong cryptography uses more secure network protocols (TLS 1.2, TLS 1.1, and TLS 1.0) and blocks protocols that are not secure. A value of 0 disables strong cryptography.

Source: https://docs.microsoft.com/en-us/dotnet/framework/network-programming/tls#schusestrongcrypto

HttpWebRequest example with error handling (C#), HttpWebRequest GET and POST C# code example with error handling. on an aspx page, just cut out the WRequest() function and catch the return string. A VB​  As you can see from Fiddler/SOAPUI it does return the SOAP fault, however using HttpWebRequest i can't capture the response. Returning 500 triggers my exception handling and Capturing the WebException doesn't expose the response. Does anyone know how to capture this fault response xml in the case of 500 Internal Server Errors. Thanks, Simon

HttpWebRequest Get.Response Error : The remote server returned , Hello I'm trying to establish a connection, but var httpResponse = (​HttpWebResponse) httpWebRequest.GetResponse(); I get the following error  HttpWebRequest.GetResponse returns 404 No Found error - ASP.NET Web Services I have a windows client app that is trying to download a file from a web server but I always get the following error when I call the GetResponse method of the Request object. The remote server returned an error: (404) Not Found.

Error system.net.httpwebrequest.getresponse(), and HttpWebResponse and iam passing cookie value and my string in Json Formart. But i get [The remote server returned an error: (500) The GetResponse method returns a WebResponse object that contains the response from the Internet resource. The actual instance returned is an HttpWebResponse, and can be typecast to that class to access HTTP-specific properties. A ProtocolViolationException is thrown in several cases when the properties set on the HttpWebRequest class are conflicting.

HttpWebRequest GetResponse: The remote server returned an error , Here Mudassar Ahmed Khan has explained how to solve the following error (​exception) occurring inside the GetResponse method of  The HttpWebRequest class throws a WebException when errors occur while accessing a resource. The WebException.Status property contains a WebExceptionStatus value that indicates the source of the error. When WebException.Status is WebExceptionStatus.ProtocolError, the Response property contains the HttpWebResponse received from the resource.

Comments
  • This code has been working great for months. However, just recently, the line that calls GetResponse is now returning the error "The underlying connection was closed". - maybe they've started rejecting calls for some reason. Have you tried setting the user agent string on the request to one of the popular browsers? Does using a proxy like Fiddler get you any more information?
  • I tried without using the SecurityProtocol as well as changing the URL but the same error is happening.
  • It looks like the site only supports TLS 1.2, so I believe you are correct that the security protocol version is likely not the issue.
  • Do you need to send credentials to the site? If not, you may try leaving the property as null. For example, request.Credentials = null;