使用 cliente.DownloadString(url);给出奇怪的方形符号 c# .net 3.5
我正在使用 webclient 从网站获取源 html 代码并将 html 放入文本框中,
但由于某种原因,在文本框中我得到了奇怪的符号
using (WebClient cliente = new WebClient())
{
textbox.Text = cliente.DownloadString(url);
}
,我正在使用 c# .net 3.5
I'm using webclient to get the source html code from websites and put the html in a textbox
but for some reason in the textbox I'm gettig weird symbol
using (WebClient cliente = new WebClient())
{
textbox.Text = cliente.DownloadString(url);
}
I'm using c# .net 3.5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些是不可打印的换行符的表示。
尝试
Those are representations of non-printable new line characters.
Try
我认为这是与编码有关的问题。
你的字符串是utf-8编码的吗?
您需要将webclient编码设置为等于网页编码(如果您管理页面,将其设置为utf-8,是一个更好的解决方案)。
http://msdn .microsoft.com/en-us/library/system.net.webclient.encoding%28v=vs.80%29.aspx
然后,我认为你不会再得到坏方块了,但是我不知道文本框使用的编码,这可能是一个问题(我再次假设它们使用 utf-8,不知道它们是否可配置)。
编辑:
没有看到你的评论,是的,我认为这些方块是 \r\n 字符,它们(可能)是用与 uft-8 不同的编码写在页面上的(所以它不是这是你的错,但这是网页开发人员造成的问题)。
´ 无法转换,必须用 string.replace 为你想要的内容(´ 是 html 用来显示一些特殊字符的)
I think that it's a problem connected to encoding.
Is your string utf-8 encoded?
You need to set the webclient encoding equals to web page enconding (if you manage the page, set it to utf-8, is a better solution).
http://msdn.microsoft.com/en-us/library/system.net.webclient.encoding%28v=vs.80%29.aspx
Then, I think you wouldn't get bad squares anymore, however I don't know encoding used by textboxes, this could be a problem (I again suppose they use utf-8, don't know if they are configurable).
EDIT:
Didn't see your comment, yes definitely I think those squares are \r\n characters, which (maybe) are written on the page with an encoding different from uft-8 (so it's not your fault but it's a problem that the webpage's developer created).
´ can't be converted, you must replace with string.replace with what you want (´ is used by html to show some special characters)