简体   繁体   English

C#使用非英语字母获取站点源代码

[英]C# Get site source code with letters other than english

I'm trying to get a site's source in C# using 我正在尝试使用C#获取网站的源代码

WebClient client = new WebClient();
string content = client.DownloadString(url);

And it gets it just fine. 它就可以了。 However, the source code contains Hebrew characters which shows like Gibbrish in content variable. 但是,源代码包含希伯来语字符,它们在内容变量中显示类似于Gibbrish。 What do I need to do for it to recognize it? 我需要做什么才能使其识别?

WebClient client = new WebClient();
client.Encoding = System.Text.UTF8Encoding.UTF8; // added
string content = client.DownloadString(url);

You have to specify the encoding, you are probably requesting ASCII by default and the content could be in UTF8. 您必须指定编码,默认情况下可能要求的是ASCII,内容可能为UTF8。 This is an example where the encoding is set to UTF8. 这是将编码设置为UTF8的示例。 If you are not sure what it is check the source manually first and then specify the encoding accordingly. 如果不确定是什么,请先手动检查源,然后相应地指定编码。 For more info see Remarks in the documentation. 有关更多信息,请参见文档中的备注

The problem is the Encoding of your WebClient. 问题是您的WebClient的编码。 MSDN says: MSDN说:

... the method uses the encoding specified in the Encoding property to convert the resource to a String. ...该方法使用Encoding属性中指定的编码将资源转换为String。

Solution: Set a specific Encoding like 解决方案:设置特定的编码,例如

client.Encoding = Encoding.UTF8;

and try it again 然后再试一次

string content = client.DownloadString(url);

UTF8 should do the trick to encode also the hebrew characters. UTF8应该可以对希伯来字符进行编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 除英语外的C#WritePrivateProfileString()值 - C# WritePrivateProfileString() value other than English language 在C#中使用英语以外的默认UI语言是否可以? - Is it ok to use a default UI language other than English in C#? C#:使用除英语之外的语言的字典的问题 - C#: Issues using dictionary with languages other than english 如何从c#代码连接到Active Directory以外的ldap数据源? - How to connect to ldap data source other than Active Directory from c# code? 如何使用c#作为注册用户获取网站源代码? - How do I get the site source code as a registered user using c#? 在C#中替换包含波斯语和英语字母的字符串 - Replace in strings which contain Persian and English letters together in C# 如何在后面的代码中检查“ localize = english”是否为“ localize = other language” ...(ASP.NET C#) - How to check if “localize=english” else if “localize=other language” … in code behind - (ASP.NET C#) C#代码问题:修订号和字母 - C# Code Problem: Revision Numbers and Letters C#-获取源代码文件正在使用或引用的类型的列表 - C# - Get List of types that a source code file is using or refering to 如何在C#中获取网页的完整源代码? - How do I get the full source code of a webpage in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM