Why could I be getting error “The underlying connection was closed” when getting an Image?

StackOverflow https://stackoverflow.com/questions/1201938

  •  05-07-2019
  •  | 
  •  

Question

I'm using a little code to grab an image given its URL, and it's working for me for all URLs I tried except one:

http://title.mximg.com/img/logo/bizrealty.com.gif

For this URL, I'm getting "The underlying connection was closed: An unexpected error occurred on a receive."

However, if you open that URL with a browser, it loads perfectly.

Apparently that error message means:

"The underlying connection was closed: An unexpected error occurred on a receive."
--Seen when the client had sent the request in its entirety and got a TCP ACK-FIN or RST from server to close the connection, without a response from server.

But I have no idea what that means :-(

The code is simply:

Dim req As System.Net.HttpWebRequest = DirectCast(WebRequest.Create(ImageURL), HttpWebRequest)
req.Method = "GET"
Dim resp As Net.HttpWebResponse = DirectCast(req.GetResponse(), Net.HttpWebResponse)

UPDATE: Setting KeepAlive to false doesn't help it. Also, it's not a timeout issue, I'm getting the error quite fast.

Any idea what could be going on?
Thanks!

Was it helpful?

Solution

I'd try updating your request settings like UserAgent or Accept. It's possible they're serving images dynamically and reject requests that don't look like normal traffic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top