我向谷歌服务器发出请求并得到鳄鱼而不是俄语,更改编码没有帮助,也许谷歌在搜索引擎上有某种棘手的编码。发送请求的类:
public static class MyWebRequest
{
public static string Get(string url, Dictionary<string, string> QueryParameters = null, Encoding encoding = null)
{
using var webClient = new WebClient();
if (encoding != null)
{
webClient.Encoding = encoding;
}
if (QueryParameters != null && QueryParameters.Count != 0)
{
foreach (var queryParameter in QueryParameters)
{
webClient.QueryString.Add(queryParameter.Key, queryParameter.Value);
}
}
return webClient.DownloadString(url);
}
}
请求本身:
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var result = MyWebRequest.Get("https://www.google.com/search",
new Dictionary<string, string>() { ["q"] = textBoxSearch.Text.Replace(' ', '+')}, Encoding.GetEncoding("UTF-8"));
Google 网站本身说它有一个 UTF-8 格式的搜索页面。可能是什么问题呢?附言 重写了对 HttpClient 的请求,但 crocozyabrs 仍然存在
public static string Get(string url, Dictionary<string, string> QueryParameters = null)
{
Task<HttpResponseMessage> response;
if (QueryParameters != null && QueryParameters.Count != 0)
{
response = client.GetAsync($"{url}?{QueryParameters.Select(s => $"{s.Key}={s.Value}").Aggregate((a, b) => $"{a}&{b}")}");
}
else
{
response = client.GetAsync(url);
}
return response.Result.Content.ReadAsStringAsync().Result;
}
你做了很多不必要的动作。
我使用 NuGet 包HtmlAgilityPack进行检查。
控制台输出
自动检测 ANSI 和 UTF-8 编码。如果你注册一个 provider
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
,那么 Win1251 会吃掉自己。WPF 中的异步按钮处理程序如下所示。