Html request via https using C # Webclient

I am trying to use various html resources using the C # WebClient class from a site on which I do not control. When I try to access URLs such as " https://archive.org/details/OTRR_In_The_Name_Of_The_Law_Singles "

I get the error: System.Net.WebException: request was aborted: could not create a secure SSL / TLS channel.

I found solutions suggesting using the following code to ignore certificate requirements and make the web client act like a browser, but I still get the same error

ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback( delegate { return true; }); using(WebClient webClient = new WebClient()) { webClient.Headers["User-Agent"] = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729)"; webClient.Headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; webClient.Headers["Accept-Language"] = "en-us,en;q=0.5"; webClient.Headers["Accept-Encoding"] = "gzip,deflate"; webClient.Headers["Accept-Charset"] = "ISO-8859-1,utf-8;q=0.7,*;q=0.7"; StreamReader sr = new StreamReader(webClient.OpenRead(inputString)); } 
+6
source share
3 answers

Read the following: http://support.microsoft.com/kb/915599

The server you are accessing does not support TLS, so you need to force it to use SSL3.

Add the following line to the call:

 ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; 

Here is a complete working example:

 using System; using System.IO; using System.Net; using System.Net.Security; using System.Security.Cryptography.X509Certificates; class Program { static void Main(string[] args) { Uri address = new Uri("https://archive.org/details/OTRR_In_The_Name_Of_The_Law_Singles"); ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate; ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 ; using (WebClient webClient = new WebClient()) { var stream = webClient.OpenRead(address); using (StreamReader sr =new StreamReader(stream)) { var page = sr.ReadToEnd(); } } } /// <summary> /// Certificate validation callback. /// </summary> private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error) { // If the certificate is a valid, signed certificate, return true. if (error == System.Net.Security.SslPolicyErrors.None) { return true; } Console.WriteLine("X509Certificate [{0}] Policy Error: '{1}'", cert.Subject, error.ToString()); return false; } 
+13
source

Just add this line to var stream = webClient.OpenRead(address);

 System.Net.ServicePointManager.ServerCertificateValidationCallback += (send, certificate, chain, sslPolicyErrors) => { return true; }; 

This should fix the SSL / TLS error.

+1
source

in .net Framework 4.0 add

 ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; //TLS 1.2 
0
source

Source: https://habr.com/ru/post/958364/


All Articles