0

When trying to download an image from a specific image hoster using HttpWebRequest I get the error

The underlying connection was closed: An unexpected error occurred on a send.

Example image I'm trying this: https://static.dyp.im/u1F6o6Q0SW/03b0aef5a48efec8ba3c1b59009e60e4.jpg

This is the code I'm using:

HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(remoteFilename);
response = request.GetResponse();

The error gets thrown on the GetResponse() line.

Downloading the image works using a browser. I also tried using the same headers a browser uses (User agent, Accept, Connection etc) but the error persists.

farid bekran
  • 2,684
  • 2
  • 16
  • 29
Froghut
  • 327
  • 1
  • 3
  • 9
  • We can't know. Most likely a scraping protection, that blocks you based on certain request headers. Or something to do with HTTPS. Try searching. – CodeCaster Dec 15 '15 at 12:21
  • Your error was because of authentication problem, check http://stackoverflow.com/a/32416630/1095390 – farid bekran Dec 15 '15 at 12:59
  • I searched a lot before posting here, none of the suggestions I found helped. I also tried setting the SecurityProtocol to TLS (also only to SSL) which did not work. – Froghut Dec 15 '15 at 14:08
  • What version of visual studio are you using? – Pikoh Dec 15 '15 at 16:08

1 Answers1

1

For what i've seen using fiddler, i think that web is using TLSv1.2. If i'm not wrong, to use tls v1.2 you need net 4.5, so you need at least visual studio 2012. Try targeting net 4.5 in your project and use something like

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback = delegate{ 
          return true;
};
Pikoh
  • 7,582
  • 28
  • 53