Silverlight:编码 webClient 流
我一直在努力让它发挥作用,但此时我感到非常沮丧。我是这个领域的初学者,所以也许我只是犯了错误。
我需要做的是获取网站 .html 并将其存储到 txt 文件中。现在的问题是这个网站是俄语的(编码windows-1251)而Silverlight只支持3种编码。因此,为了绕过这个限制,我得到了一个编码类,它将流传输到字节数组中,然后尝试从文本中提取正确编码的字符串。问题在于
1) 我试图确保 webClient 收到 Unicode 编码流,因为其他的似乎没有创建可检索的字符串,但它似乎仍然不起作用。
WebClient wc = new WebClient();
wc.Encoding = System.Text.Encoding.Unicode;
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_LoadCompleted);
wc.DownloadStringAsync(new Uri(site));
2)我担心当我使用streamWriter将html存储到txt文件中时,编码再次以某种方式搞砸了。 3) 编码类没有发挥其作用。
Encoding rus = Encoding.GetEncoding(1251);
Encoding eng = Encoding.Unicode;
byte[] bytes = rus.GetBytes(string);
textBlock1.Text = eng.GetString(bytes);
任何人都可以在这件事上提供任何帮助吗?这对我的项目造成巨大损害。提前致谢,
I've been trying to get this to work, but I'm very frustrated at this point. I am a beginner in this field, so maybe I'm just making mistakes.
What I need to do is to take in a website .html and store it into a txt file. Now the problem is that this website is in Russian (encoding windows-1251) and Silverlight only supports 3 encodings. So in order to bypass that limitation, I got my hands on an encoding class that transfers the stream into a byte array and then tries to pull the correctly encoded string from the text. The problem with this is that
1) I try to ensure that webClient recieves a Unicode encoded stream, because the other ones do not seem to create a retrievable string, but it still doesn't seem to work.
WebClient wc = new WebClient();
wc.Encoding = System.Text.Encoding.Unicode;
wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_LoadCompleted);
wc.DownloadStringAsync(new Uri(site));
2) I fear that when I store the html into a txt file using streamWriter, the encoding is, yet again, somehow screwed up.
3) The encoding class is not doing its job.
Encoding rus = Encoding.GetEncoding(1251);
Encoding eng = Encoding.Unicode;
byte[] bytes = rus.GetBytes(string);
textBlock1.Text = eng.GetString(bytes);
Can anyone offer any help on this matter? This huge detriment to my project. Thanks in advance,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您想要处理与 Silverlight 不同的编码,因此您应该首先使用
OpenReadAsync
和OpenReadCompleted
下载。现在您应该能够获取事件参数
Result
属性提供的Stream
并将其直接提供给您已获取的编码组件以生成正确的字符串结果。Since you want to handle an encoding alien to Silverlight you should start with downloading using
OpenReadAsync
andOpenReadCompleted
.Now you should be able to take the
Stream
provided by the event argsResult
property and supply it directly to the encoding component you have acquired to generate the correct string result.