-1

我正在尝试使用以下代码读取 c# 中 https url 的 html 源代码:

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

在此处输入图像描述

这对我不起作用,因为我得到了编码的 html 字符串。我尝试使用 HtmlAgilityPack 但没有帮助。

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(htmlString);
4

2 回答 2

3

该 URL 正在返回一个 gzip 压缩字符串。WebClient默认情况下不支持此功能,因此您需要HttpWebRequest转而使用底层类。feroze 在这里公然剽窃答案 -通过 WebClient.DownloadData 自动解压缩 gzip 响应

class MyWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        HttpWebRequest request = base.GetWebRequest(address) as HttpWebRequest;
        request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
        return request;
    }
}
于 2013-05-30T06:31:16.907 回答
0
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
WebClient webClient = new WebClient();
string htmlString = webClient.DownloadString(url);
于 2013-05-30T06:12:31.550 回答