asp.net - 奇怪的维基百科mojibake(错误编码)

标签 asp.net character-encoding

这是第一个对我来说有问题的维基百科页面。当我使用 HttpWebResponse.GetResponseStream() 打开此页面 https://en.wikipedia.org/wiki/London ,里面都是mojibake。但我的浏览器可以毫无问题地对其进行编码。

我使用了三种方法来下载文本文件。他们都得到不同的文件。

第一种方法下载了274,851字节的文件

    string TargetUri = "https://en.wikipedia.org/wiki/London";

    HttpWebRequest queryPage = (HttpWebRequest)WebRequest.Create(TargetUri);

    queryPage.Credentials = CredentialCache.DefaultCredentials;

    using (HttpWebResponse response = (HttpWebResponse)queryPage.GetResponse())
    {

        using (Stream PageRawCode = response.GetResponseStream())
        {
            using (MemoryStream PageRawCodeDuplicate = new MemoryStream())
            {
                byte[] buffer = new byte[1024];
                int ByteCount;
                do
                {
                    ByteCount = PageRawCode.Read(buffer, 0, buffer.Length);
                    PageRawCodeDuplicate.Write(buffer, 0, ByteCount);
                } while (ByteCount > 0);

                PageRawCodeDuplicate.Seek(0, SeekOrigin.Begin);

                using (StreamReader CodeInUTF8 = new StreamReader(PageRawCodeDuplicate))
                {
                    string PageText = CodeInUTF8.ReadToEnd();
                    using (StreamWriter sw = new StreamWriter(@"E:\My Documents\Desktop\london1.html"))
                    {
                        sw.Write(PageText);
                    }
                }
            }
        }
    }

第二种方法是

    WebClient myWebClient = new WebClient();
    myWebClient.DownloadFile("https://en.wikipedia.org/wiki/London", @"E:\My Documents\Desktop\london2.html");

此方法仅下载了152.297字节的文件

第三种方法是打开https://en.wikipedia.org/wiki/London ,并保存源代码文件。该方法将得到一个1,746,420字节的文件

我不明白为什么使用不同的方法获取文本文件会有这样的差异。

我使用了 ASCII、BigEndianUnicode、Unicode、UTF32、UTF7、UTF8 来读取前 2 个文件。它们都没有正确显示代码。

然后我读取了文件的十六进制代码。 london1.html 的前 32 个字符是

1FEFBFBD0800000000000003EFBFBDEF

london2.html 的前 32 个字符是

1F8B0800000000000003ECFD4B8F1C49

显然他们不是<!DOCTYPE html>

这两个文件是什么?我什至不知道如何检查它们。

最佳答案

您的代码中有一个简单的问题。您忘记刷新内存流。我还添加了第二个解决方案,它不会首先复制内存中的流...

如果我运行这个稍微修改过的代码,我会得到一个完整的 html 文件:

using (HttpWebResponse response = (HttpWebResponse)queryPage.GetResponse())
{

    using (Stream PageRawCode = response.GetResponseStream())
    {
        using (MemoryStream PageRawCodeDuplicate = new MemoryStream())
        {
            byte[] buffer = new byte[1024];
            int ByteCount;
            do
            {
                ByteCount = PageRawCode.Read(buffer, 0, buffer.Length);
                PageRawCodeDuplicate.Write(buffer, 0, ByteCount);
            } while (ByteCount > 0);

            // FLUSH!
            PageRawCodeDuplicate.Flush();

            PageRawCodeDuplicate.Seek(0, SeekOrigin.Begin);

            // Pick an encoding here
            using (StreamReader CodeInUTF8 = new StreamReader(
                                   PageRawCodeDuplicate, Encoding.UTF8))
            {
                string PageText = CodeInUTF8.ReadToEnd();
                using (StreamWriter sw = new StreamWriter(@"london1.html"))
                {
                    sw.Write(PageText);
                }
            }
        }
    }
}

流的直接复制

using (HttpWebResponse response = (HttpWebResponse)queryPage.GetResponse())
{
    using (Stream PageRawCode = response.GetResponseStream())
    {
            using (StreamReader CodeInUTF8 = new StreamReader(
                                                      PageRawCode, Encoding.UTF8))
            {
                using (StreamWriter sw = new StreamWriter(@"london1.html"))
                {
                    while (!CodeInUTF8.EndOfStream)
                    {
                        sw.WriteLine(CodeInUTF8.ReadLine());
                    }
                 }
            }
    }
}

关于asp.net - 奇怪的维基百科mojibake(错误编码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17895548/

相关文章:

ASP.NET 错误 : Content controls are allowed only in content page that references a master page

c# - 停止 GridView 标题滚动

c# - 如何使网站状态对所有平台可用

python-2.7 - 如何在 Python 2.x 中获取系统默认编码?

c# - iOS webapp 启动画面缓存

asp.net - webGrid.Column 不允许更改列宽

javascript - 在 Firefox 中出现 "undeclared character encoding"错误

java - 无法在 Excel 中使用 UTF8/UTF16 编码写入多字节字符

C# Encoding.UTF8 弄乱了字节 []

javascript - 在 PDF 中显示 UTF-8 字符