none
error when try to download a webpage RRS feed

  • Question

  • i am using Visual Studio 2015

    i am getting error message about line 23 for following code

    22)            WebClient client = new WebClient();
    
    23)            string reply = client.DownloadString("http://msdn.microsoft.com");
     
    24)            File.WriteAllText(@"C:\test.txt", reply);

    Unhandled Exception: System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.
       at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
       at System.Net.WebClient.DownloadString(Uri address)
       at System.Net.WebClient.DownloadString(String address)
       at helloworld.Program.Main(String[] args) in C:\Users\hp\Documents\Visual Studio 2015\Projects\helloworld\helloworld\Program.cs:line 23


    Monday, April 6, 2020 8:43 AM

Answers

  • The site is HTTPS, not HTTP. Normally this triggers a 302 (or equivalent) redirect response which the browser follows immediately. WebClient will follow this redirect by default. However this particular site was replaced with the new Docs site late last year so now it redirects to https://docs.microsoft.com/en-us/.

    I had no issues pulling the site using the code you posted so there are a couple of possibilities here. Most likely you do not have the appropriate TLS encryption protocol installed. This should happen naturally with Windows but you need to opt in to using higher SSL protocols in the Internet Options settings.

    As for your later problem where you are using HTTPS but you're getting funny characters I'm going to wager it is a translation thing. Since you aren't passing any HTTP headers the site doesn't know what browser or language to return data in. This site is also heavily based upon external content so it is quite possible you're getting a mix of results. If you load the page in the browser with the Dev tools (F12) running you can see all the errors/warnings on this page. I would recommend you take a look at the original request sent (Network tab) and then add the appropriate headers into your call until you get the results you want. The site might be looking for specific stuff.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by makhan080372 Tuesday, April 7, 2020 12:23 PM
    Monday, April 6, 2020 1:46 PM
    Moderator
  • I think he is trying to open the downloaded "test.html" directly on browser.

    The downloaded file contains "gzip"-ed payload, it's pretty normal to see the "text" like this.

    With HttpWebRequest you can enable .AutomaticDecompression, with WebClient you need to pass the content to GZipStream to decompress it to normal HTML content.

    • Marked as answer by makhan080372 Tuesday, April 7, 2020 12:23 PM
    Tuesday, April 7, 2020 1:27 AM
    Answerer
  • Hi,

    Thank you for posting here.

    For your initial error, I did not encounter it when using your code.

    I modified the Internet Options settings to try to reproduce this error but still failed.

    But when I searched for information related to this issue, many people said that this error can be solved by adding the following two lines of code.

    I am not sure if it will work for you, but you can give it a try.

              ServicePointManager.Expect100Continue = true;
              ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    
                WebClient client = new WebClient();
    
                string reply = client.DownloadString("https://docs.microsoft.com/en-us/");
            
                File.WriteAllText(@"D:\test.txt", reply);

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by makhan080372 Tuesday, April 7, 2020 12:23 PM
    Tuesday, April 7, 2020 2:36 AM

All replies

  • i also tried 

    string address = "https://msdn.microsoft.com"

    still same error message

    also getting weird input when trying to download http://yahoo.com

    1)          string address = "http://yahoo.com";
    
    2)            WebClient client = new WebClient();
    
    3)            string reply = client.DownloadString(address);
    
    4)            File.WriteAllText(@"C:\test.html", reply);


    some of output is shown below

    ‹       ì½i{I²ú}~-§¥1 Ú)¤Q÷­ÈB+’,µúú)ª
    (‰ÍT±zt~ûÌ¬%7d{–sÛmoDî‘‘[ä?~Ù;Ûmܝïç:Q¯ûÛ?п¹ÀÛYs¢A/p×r]§ßÞYóû…뫵œÛuÂ0¡åzsÝtǽ~˜ëã(w> #
    Ã\kÿ&ú×ó[θåzA?(Œ;…A?ÿFƒaÓ¡Ýh”óüð Br{ñgó)Ò5×r‚nè´ü5Hšïx¿ý%ÿý£çGŽ§à“µO…ëJawÐ:QÐìúÒA?òûÑÎZmÇ÷Ú²Ix‰€(ˆºþowNg0øÇ&ù!
    ¼~XŽü–¹9t)Ùƒ>ûnÐʍüî΍_ËuàÛÎÚæfXœ½vÑô88@AVßw£W€—Èö‘Û)ÎQN^Ãjy<n蛾3,6Ý7Eö>yŒmð¦˜Váå1LÏœaPœé¾)®œòö¹KšL¡1ҍ3ògÑ&êrÛ9·ãŒB?ÚG­‚¶V"©ïôüµIàO‡ƒQDñO/êìxþ$pýþ‘ÏA‹§[]§ëï¨E燒uüž­¹;Q¢Þµð<¸:Ãa7p¡_
    ú…¾3iŽ£¾ñì#ß[ÍÚºþî·Ä‰k=§M—V1[tgÙt'›Pm3¸ýÍ ±„›¸n>Ç
    èswЇý6#›ÇÖ«S9ÃB'hwºð—®¾À×wQÅŒ|¿Oáæ~È›£Á4ôG½G§…Š”g裸9¸„V†,´ëÌAYsØV%ébѸyð`äúÒ6

    Monday, April 6, 2020 8:50 AM
  • The site is HTTPS, not HTTP. Normally this triggers a 302 (or equivalent) redirect response which the browser follows immediately. WebClient will follow this redirect by default. However this particular site was replaced with the new Docs site late last year so now it redirects to https://docs.microsoft.com/en-us/.

    I had no issues pulling the site using the code you posted so there are a couple of possibilities here. Most likely you do not have the appropriate TLS encryption protocol installed. This should happen naturally with Windows but you need to opt in to using higher SSL protocols in the Internet Options settings.

    As for your later problem where you are using HTTPS but you're getting funny characters I'm going to wager it is a translation thing. Since you aren't passing any HTTP headers the site doesn't know what browser or language to return data in. This site is also heavily based upon external content so it is quite possible you're getting a mix of results. If you load the page in the browser with the Dev tools (F12) running you can see all the errors/warnings on this page. I would recommend you take a look at the original request sent (Network tab) and then add the appropriate headers into your call until you get the results you want. The site might be looking for specific stuff.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by makhan080372 Tuesday, April 7, 2020 12:23 PM
    Monday, April 6, 2020 1:46 PM
    Moderator
  • Thanks Michael Taylor for your kind reply

    however i dont know what to look for in the following image




    Monday, April 6, 2020 2:02 PM
  • Where did that image come from? It looks like the browser window but that isn't HTML. You should be able to load the actual site in the browser using the URL. If this is what you're getting then I suspect there is something with the site and your region. You might try a different browser or a different site altogether.

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, April 6, 2020 2:06 PM
    Moderator
  • I think he is trying to open the downloaded "test.html" directly on browser.

    The downloaded file contains "gzip"-ed payload, it's pretty normal to see the "text" like this.

    With HttpWebRequest you can enable .AutomaticDecompression, with WebClient you need to pass the content to GZipStream to decompress it to normal HTML content.

    • Marked as answer by makhan080372 Tuesday, April 7, 2020 12:23 PM
    Tuesday, April 7, 2020 1:27 AM
    Answerer
  • Hi,

    Thank you for posting here.

    For your initial error, I did not encounter it when using your code.

    I modified the Internet Options settings to try to reproduce this error but still failed.

    But when I searched for information related to this issue, many people said that this error can be solved by adding the following two lines of code.

    I am not sure if it will work for you, but you can give it a try.

              ServicePointManager.Expect100Continue = true;
              ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    
                WebClient client = new WebClient();
    
                string reply = client.DownloadString("https://docs.microsoft.com/en-us/");
            
                File.WriteAllText(@"D:\test.txt", reply);

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by makhan080372 Tuesday, April 7, 2020 12:23 PM
    Tuesday, April 7, 2020 2:36 AM
  • Timon


    Thanks Timon and everybody else
    Tuesday, April 7, 2020 12:24 PM