0

On one particular website on my web server WebRequests have started throwing the following error: "The underlying connection was closed: An unexpected error occurred on a send."

  • My asp.NET website is running framework version 4.8.
  • I am requesting a page on the same website that is doing the request.
  • This code has been in place for years and has suddenly stopped working on this website
  • This same code works fine on a different website running on the same webserver

Request code:

Dim myReq As System.Net.WebRequest = System.Net.WebRequest.Create("https://example.com")
myReq.Method = "GET"

myReq.ContentType = "text/html; encoding='utf-32'"


Dim wr As System.Net.WebResponse = myReq.GetResponse()
Dim receiveStream As System.IO.Stream = wr.GetResponseStream()
Dim reader As System.IO.StreamReader = New System.IO.StreamReader(receiveStream, Encoding.UTF8)
Dim content As String = reader.ReadToEnd()

I have tried adding the following to the page doing the request, and the global.asax, but it did not work. It should not be required, though, because I am using .net 4.8:

System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Benjo
  • 179
  • 1
  • 8
  • 1
    Have you checked the DNS settings / HOSTS file entry for the affected site? – Richard Deeming Oct 04 '21 at 15:45
  • Think you might be on the right track with the security protocol. Have a look at https://stackoverflow.com/questions/22627977/the-underlying-connection-was-closed-an-unexpected-error-occurred-on-a-send which suggests a few other additions – Hursey Oct 04 '21 at 19:23
  • @RichardDeeming you are a diamond. That was the issue - for three days I've been looking at everything - as soon as you said that, I remembered adding a hosts value last week. Removed it and the error went away. Thank you! If you add as an answer I'll mark it. – Benjo Oct 05 '21 at 07:40

1 Answers1

1

As mentioned in the comments, this is often an issue with the internal DNS or a HOSTS file entry. Either the server is resolving the site's domain to the wrong internal address, or it's trying to access the site's public address and confusing the router.

Richard Deeming
  • 29,830
  • 10
  • 79
  • 151