我使用DownloadString
类中的WebClient
从页面中读取内容,然后使用StreamWriter
类将其内容写入静态HTML文件。在我正在阅读的页面上,有一个内联的javascript方法,只需设置一个锚元素的OnClick
属性来设置window.location = history.go(-1);
我就可以了当我查看静态HTML页面时,会看到一个奇怪的字母,显示在动态网页上不会出现。
WebClient& SteamWriter代码
using (var client = new WebClient())
{
var html = client.DownloadString(url);
//This constructor prepares a StreamWriter (UTF-8) to write to the specified file or will create it if it doesn't already exist
using (var stream = new StreamWriter(file, false, Encoding.UTF8))
{
stream.Write(html);
stream.Close();
}
}
动态网页的HTML代码段
<span>Sorry, but something went wrong on our end. Click <a href="#" onclick="window.location.href = history.go(-1);">here</a> to go back to the previous page.</span>
静态网页的HTML代码段
<span>Sorry, but something went wrong on our end. Â Click <a href="#" onclick="window.location.href = history.go(-1);">here</a> to go back to the previous page.</span>
我在想添加Encoding.UTF8
参数可以解决这个问题,但它似乎没有帮助。我需要做某种额外的编码或解码吗?或者我是否完全错过了此类操作所需的其他内容?
答案 0 :(得分:0)
我更新了WebClient以在UTF8中进行编码,因为它将资源转换为字符串,似乎已经解决了这个问题。
using (var client = new WebClient())
{
client.Encoding = System.Text.Encoding.UTF8;
var html = client.DownloadString(url);
//This constructor prepares a StreamWriter (UTF-8) to write to the specified file or will create it if it doesn't already exist
using (var stream = new StreamWriter(file, false, Encoding.UTF8))
{
stream.Write(html);
stream.Close();
}
}