它在处理大于和小于符号和引号等方面存在问题。
您应该尝试HttpUtility.HtmlDecode以解决该问题。
剩下的,你应该试试这个(来源):
private void SaveWebPage_as_HTML()
{
// Initialize the WebRequest.
string urlToConvert = (System.Web.HttpContext.Current.Request.Url).ToStr ing();
WebRequest myRequest = WebRequest.Create(urlToConvert);
// Return the response.
WebResponse myResponse = myRequest.GetResponse();
// Obtain a 'Stream' object associated with the response object.
Stream ReceiveStream = myResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipe the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(ReceiveStream, encode, true, 255);
// Read 256 charcters at a time.
Char[] read = new Char[256];
int count = readStream.Read(read, 0, 256);
using (StreamWriter sw = new StreamWriter("output.html"))
{
while (count > 0)
{
// Dump the 256 characters on a string and display the string onto the console.
String str = new String(read, 0, count);
sw.Write(str);
count = readStream.Read(read, 0, 256);
}
}
// Close the response to free resources.
myResponse.Close();
}
密切注意使用的编码(这里是 utf-8),因为这可能会导致“可读性”问题。