-1

i am trying to read the html source code of a https url in c# with the following code:

 WebClient webClient = new WebClient();
 string htmlString = w.DownloadString("https://www.targetUrl.com");

enter image description here

this doesn't work for me as i get encoded html string. I tried using HtmlAgilityPack but with no help.

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(htmlString);
Newton Sheikh
  • 1,376
  • 2
  • 19
  • 42
kavita verma
  • 75
  • 1
  • 10
  • 1
    What does this mean `this doesn't work for me as i get encoded html string`? – I4V May 30 '13 at 06:09
  • means its not work for HTTPS link as https://www.targetUrl.com – kavita verma May 30 '13 at 06:11
  • `WebClient.DownloadString` doesn't need to do anything special to download from a https address. What do you mean "encoded"? How do you know it's encoded? What does it look like? – Snixtor May 30 '13 at 06:15
  • We do not have crystal ball here. What do you expect? what do you see? What exception do you get? – I4V May 30 '13 at 06:15

2 Answers2

3

That URL is returning a gzip compressed string. WebClient doesn't support this by default, so you'll want to go down to the underlying HttpWebRequest class instead. Blatant rip-off of the answer by feroze over here - Automatically decompress gzip response via WebClient.DownloadData

class MyWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        HttpWebRequest request = base.GetWebRequest(address) as HttpWebRequest;
        request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
        return request;
    }
}
Community
  • 1
  • 1
Snixtor
  • 4,239
  • 2
  • 31
  • 54
0
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
WebClient webClient = new WebClient();
string htmlString = webClient.DownloadString(url);
Guy Luz
  • 3,372
  • 20
  • 43
Damith
  • 62,401
  • 13
  • 102
  • 153
  • You should probably add some comment with your code to make it clear how is your answer relevant compared to the already accepted answer. – Corentin Pane Dec 19 '19 at 15:28