none
WebClient DownloadString(Url) com strange characters RRS feed

  • Question

  • Hello,

    I trying to get the source code of a page. Until yesterday, it was working.

    using (System.Net.WebClient client = new System.Net.WebClient())
    {
        string Url= "https://fnet.bmfbovespa.com.br/fnet/publico/exibirDocumento?id=66464#toolbar=0";
        client.Headers["User-Agent"] = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.12) Gecko/20100824 Firefox/3.5.12x";
        client.Encoding = Encoding.UTF8;
        Console.WriteLine(client.DownloadString(Url));               
    }

    But the site has done a maintenance and, after that, it is not working anymore. The output now is strange characters: "PGh0bWw+CjxoZWFkPgo8TUVUQSBodHRwLWVxdWl2PSJ... 

    I searched about encoding/decoding but without success. Anyone can help me?

    Thank you.

    Sunday, January 19, 2020 3:39 PM

Answers

  • I tried it, and it is returning Base64 text surrounded by quotes.

    Here is an example for how to decode it:

    using System;
    using System.Net;
    using System.Text;
    
    namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
                ServicePointManager.Expect100Continue = true;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    
                using (System.Net.WebClient client = new System.Net.WebClient())
                {
                    string Url = "https://fnet.bmfbovespa.com.br/fnet/publico/exibirDocumento?id=66464#toolbar=0";
                    client.Headers["User-Agent"] = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.12) Gecko/20100824 Firefox/3.5.12x";
                    client.Encoding = Encoding.UTF8;
                    string encoded = client.DownloadString(Url);
                    byte[] converted = Convert.FromBase64String(encoded.Substring(1, encoded.Length - 2));
                    string decoded = Encoding.UTF8.GetString(converted);
                    Console.WriteLine(decoded);
                }
            }
        }
    }
    

    Note that I had to add a couple of instructions to alter the security because otherwise I got a "cannot create SSL/TLS..." error.

    Sunday, January 19, 2020 6:46 PM
    Moderator

All replies

  • I tried it, and it is returning Base64 text surrounded by quotes.

    Here is an example for how to decode it:

    using System;
    using System.Net;
    using System.Text;
    
    namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
                ServicePointManager.Expect100Continue = true;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    
                using (System.Net.WebClient client = new System.Net.WebClient())
                {
                    string Url = "https://fnet.bmfbovespa.com.br/fnet/publico/exibirDocumento?id=66464#toolbar=0";
                    client.Headers["User-Agent"] = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.12) Gecko/20100824 Firefox/3.5.12x";
                    client.Encoding = Encoding.UTF8;
                    string encoded = client.DownloadString(Url);
                    byte[] converted = Convert.FromBase64String(encoded.Substring(1, encoded.Length - 2));
                    string decoded = Encoding.UTF8.GetString(converted);
                    Console.WriteLine(decoded);
                }
            }
        }
    }
    

    Note that I had to add a couple of instructions to alter the security because otherwise I got a "cannot create SSL/TLS..." error.

    Sunday, January 19, 2020 6:46 PM
    Moderator
  • It worked.

    You are the best! I have to study what is Base64 text.

    Thank you very much for your answer.

    Sunday, January 19, 2020 7:00 PM
  • Hi StockCanadian,

    It seems that your problem has been solved. If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    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.

    Monday, January 20, 2020 1:18 AM