gpt4 book ai didi

C# WebClient DownloadString 返回乱码

转载 作者:太空狗 更新时间:2023-10-30 01:23:56 26 4
gpt4 key购买 nike

我正在尝试查看 http://simpledesktops.com/browse/desktops/2012/may/17/where-the-wild-things-are/ 的来源使用代码:

String URL = "http://simpledesktops.com/browse/desktops/2012/may/17/where-the-wild-things-are/";

WebClient webClient = new WebClient();

webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows; Windows NT 5.1; rv:1.9.2.4) Gecko/20100611 Firefox/3.6.4");
webClient.Encoding = Encoding.GetEncoding("Windows-1255");

string download = webClient.DownloadString(URL);

webClient.Dispose();

Console.WriteLine(download);

当我运行它时,控制台返回一堆看起来解码不正确的废话。

我也尝试添加 header 但无济于事:

webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows; Windows NT 5.1; rv:1.9.2.4) Gecko/20100611 Firefox/3.6.4");    
webClient.Headers.Add("Accept-Encoding", "gzip,deflate");

其他网站都返回了正确的 html 源。我还可以通过 Chrome 查看页面的源代码。这是怎么回事?

最佳答案

该 URL 的响应是 gzip 压缩的,您应该将其解压缩或设置空的 Accept-Encoding header ,您不需要该用户代理字段。

  String URL = "http://simpledesktops.com/browse/desktops/2012/may/17/where-the-wild-things-are/";    
WebClient webClient = new WebClient();
webClient.Headers.Add("Accept-Encoding", "");
string download = webClient.DownloadString(URL);

关于C# WebClient DownloadString 返回乱码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10778477/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com