【问题标题】:How can i search a hebrew word from a website using c#如何使用 c# 从网站中搜索希伯来语单词
【发布时间】:2019-06-20 21:26:40
【问题描述】:

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

感谢您的帮助:)

【问题讨论】:

  • 检查This
  • 当您在调试器中单步执行代码时,text 中的内容是什么,即您在检索数据或搜索数据时遇到问题?

标签: c# html if-statement


【解决方案1】:

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

希望这会有所帮助。

【讨论】:

  • 我通过 JDoodle 运行了这两个代码示例并得到了错误。代码是否要放在方法中?
  • 是的,代码必须在方法中,例如 Main。我自己在 jdoodle 上尝试过,我得到了“System.Net.WebException:错误:NameResolutionFailure”,这可能意味着他们不允许来自其服务内部的 Web 请求(出于安全原因,这是有道理的)。如果您在 Windows 窗体应用程序中本地尝试它,它将起作用。您还需要“使用 System.Net;”在 .cs 文件的顶部。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多