views:

163

answers:

1

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!

+3  A: 

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.

Jon
That's it. Just add User-Agent header and it will work.
Darin Dimitrov
+1 for extra cleverness! Thank you! The User Agent did it.
Daniel Magliola