mercredi 11 mars 2020

Is there a way to easily web scrap and solve the SSL/TLS connection error in the code below?

This is a problem I am having when I try to run a simple web scrapping code.

I will appreciate any help, even other (easy) methods to try to get the same result.

I get the error: The SSL/TLS connection could not be established.

It is from System.Net.Http.HttpRequestException.

Many Thanks, Again.

(The comments are some of the solutions mentioned on other threads and I have tried them with the same problem occurring).

   class Program
        {
            static void Main(string[] args)
            {
            Console.WriteLine("Hello World!");

            var url = string.Format("https://www.worldometers.info/coronavirus/");

            getHtmlAsync(url);

            Console.Read();

        }

        private static async void getHtmlAsync(string url)
        {
         // ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
         //System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
           // ServicePointManager.Expect100Continue = true;
           // ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
           // ServicePointManager.DefaultConnectionLimit = 9999;

        //    client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate");
        //    client.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.5");
        //    client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0");
         //   client.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

            Console.WriteLine("I am here");

            var client = new HttpClient();
            var html = await client.GetStringAsync(url);
            var htmlDocument = new HtmlAgilityPack.HtmlDocument();
            htmlDocument.LoadHtml(html);

       }
}



Aucun commentaire:

Enregistrer un commentaire