I am using WebClient to get data from a web resource thus:

var wc = new System.Net.WebClient();
var stream = wc.OpenRead("http://...");
// etc..

It used to work until recently, when the server would forcibly close the connection.

Based on this StackOverflow answer, I added the ServicePointManager setting thus:

var wc = new System.Net.WebClient();
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var stream = wc.OpenRead("http://...");
// etc..

… and it once again works as it did before.

I get that TLS is relevant to HTTPS and the effect of the setting is to include TLS1.2 in the handshake, and that the host site must have been recently updated to reject the older vulnerable protocol, but why is it necessary for ordinary, non-secure HTTP?

1 Answers

1
Alexandre Fenyo On

AllowAutoRedirect is set to true by default in WebClient instances. Therefore, the request automatically follows redirection responses from your server.

Simply set AllowAutoRedirect to false and you will not follow any redirection, so you will not have to deal with SSL/TLS handshakes.

Of course, if your server does not want to serve your request with HTTP, you will not get the content you are looking for. Anyway, setting AllowAutoRedirect to false will help you confirm that the behaviour you encounter is due to a redirect.