如何使用C#从网站搜索希伯来语单词

时间:2019-01-27 20:17:28

标签: c# html if-statement

我正在尝试使用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");
        }

感谢您的帮助:)

1 个答案:

答案 0 :(得分:2)

问题似乎是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

希望这会有所帮助。