首页 > 解决方案 > 如何使用 c# 从网站中搜索希伯来语单词

问题描述

我试图使用 c# 在网站中搜索希伯来语单词,但我无法弄清楚。这是我当前尝试使用的状态代码:

var client = new WebClient();
        Encoding encoding = Encoding.GetEncoding(1255);
        var text = client.DownloadString("http://shchakim.iscool.co.il/default.aspx");

        if (text.Contains("ביטול"))
        {
            MessageBox.Show("idk");
        }

谢谢你的帮助 :)

标签: c#htmlif-statement

解决方案


问题似乎是 WebClient 在将响应转换为字符串时未使用正确的编码,您必须将 WebClient.Encoding 属性设置为服务器的预期编码,才能正确进行此转换。

我检查了来自服务器的响应,它是使用 utf-8 编码的,下面的更新代码反映了这种变化:

using (var client = new WebClient())
{
    client.Encoding = System.Text.Encoding.UTF8;

    var text = client.DownloadString("http://shchakim.iscool.co.il/default.aspx");

    // The response from the server doesn't contains the word ביטול, therefore, for demo purposes I changed it for שוחרות which is present in the response.
    if (text.Contains("שוחרות"))
    {
        MessageBox.Show("idk");
    }
}

在这里您可以找到有关 WebClient.Encoding 属性的更多信息: https ://docs.microsoft.com/en-us/dotnet/api/system.net.webclient.encoding?view=netframework-4.7.2

希望这可以帮助。


推荐阅读