gpt4 book ai didi

c# - 无法下载 utf-8 网页内容

转载 作者:行者123 更新时间:2023-11-30 13:38:47 25 4
gpt4 key购买 nike

我有简单的代码可以从越南网站获得响应:http://vnexpress.net ,但是有一个小问题。第一次,它下载正常,但之后,内容包含这样的未知符号:�\b\0\0\0\0\0\0�\a`I�%&/m....有什么问题?

    string address = "http://vnexpress.net";
WebClient webClient = new WebClient();
webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11 AlexaToolbar/alxg-3.1");
webClient.Encoding = System.Text.Encoding.UTF8;
return webClient.DownloadString(address);

最佳答案

您会发现响应是 GZip 压缩的。似乎没有办法使用 WebClient 下载它,除非您创建派生类并修改基础 HttpWebRequest 以允许自动解压缩。

这是你应该如何做到的:

    public class MyWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
var req = base.GetWebRequest(address) as HttpWebRequest;
req.AutomaticDecompression = DecompressionMethods.GZip;
return req;
}
}

并使用它:

string address = "http://vnexpress.net";
MyWebClient webClient = new MyWebClient();
webClient.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11 AlexaToolbar/alxg-3.1");
webClient.Encoding = System.Text.Encoding.UTF8;
return webClient.DownloadString(address);

关于c# - 无法下载 utf-8 网页内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15034771/

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