Home > database >  asp.NET 4.8 WebRequest errors with "The underlying connection was closed: An unexpected error o
asp.NET 4.8 WebRequest errors with "The underlying connection was closed: An unexpected error o

Time:10-05

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

CodePudding user response:

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.

  • Related