c# - 如何使用 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");
}
谢谢你的帮助 :)
解决方案
问题似乎是 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
希望这可以帮助。
推荐阅读
- python - 覆盖 __new__ 以返回初始化对象时如何使用上下文
- visual-c++ - 如何在 Visual std C++ 上编写一些公式(在“类”类型中)
- amazon-web-services - Fargate 的公共 IP 上的 HTTPS - 可能吗?
- javascript - 如何在javascript中使用图像的src更改标识符的url?
- java - 在 RecyclerView 中单击项目的问题
- javascript - 脚本将“=s1400”附加到图像 src 路径,现在图像不会加载
- java - 放心:获取 org.apache.http.ConnectionClosedException:内容长度分隔的消息正文过早结束
- laravel - 如何在 ubuntu 上设置虚拟主机
- javascript - 如何在不向主容器添加滚动的情况下将滚动设置为兄弟孩子
- spring-boot - spring boot 项目作为另一个 spring-boot 项目中的依赖项