c# - HttpWebResponse 的编码问题

标签 c# encoding

下面是一段代码:

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(request.RawUrl);
WebRequest.DefaultWebProxy = null;//Ensure that we will not loop by going again in the proxy
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
string charSet = response.CharacterSet;
Encoding encoding;
if (String.IsNullOrEmpty(charSet))
encoding = Encoding.Default;
else
encoding = Encoding.GetEncoding(charSet);

StreamReader resStream = new StreamReader(response.GetResponseStream(), encoding);
return resStream.ReadToEnd();

问题是如果我测试:http://www.google.fr

所有“é”都无法正常显示。我已经尝试将 ASCII 更改为 UTF8,但它仍然显示错误。我已经在浏览器中测试了 html 文件,浏览器可以很好地显示 html 文本,所以我很确定问题出在我用来下载 html 文件的方法中。

我应该改变什么?

删除了无效的 ImageShack 链接

更新 1:代码和测试文件已更改

最佳答案

CharacterSet 默认为“ISO-8859-1”,如果它没有在服务器的内容类型 header 中指定(不同于 HTML 中的“charset”元标记)。 我将 HttpWebResponse.CharacterSet 与 HTML 的字符集属性进行比较。如果它们不同 - 我使用 HTML 中指定的字符集再次重新读取页面,但这次使用正确的编码。

查看代码:

    string strWebPage = "";
    // create request
    System.Net.WebRequest objRequest = System.Net.HttpWebRequest.Create(sURL);
    // get response
    System.Net.HttpWebResponse objResponse;
    objResponse = (System.Net.HttpWebResponse)objRequest.GetResponse();
    // get correct charset and encoding from the server's header
    string Charset = objResponse.CharacterSet;
    Encoding encoding = Encoding.GetEncoding(Charset);
    // read response
    using (StreamReader sr = 
           new StreamReader(objResponse.GetResponseStream(), encoding))
    {
        strWebPage = sr.ReadToEnd();
        // Close and clean up the StreamReader
        sr.Close();
    }

    // Check real charset meta-tag in HTML
    int CharsetStart = strWebPage.IndexOf("charset=");
    if (CharsetStart > 0)
    {
        CharsetStart += 8;
        int CharsetEnd = strWebPage.IndexOfAny(new[] { ' ', '\"', ';' }, CharsetStart);
        string RealCharset = 
               strWebPage.Substring(CharsetStart, CharsetEnd - CharsetStart);

        // real charset meta-tag in HTML differs from supplied server header???
        if(RealCharset!=Charset)
        {
            // get correct encoding
            Encoding CorrectEncoding = Encoding.GetEncoding(RealCharset);

            // read the web page again, but with correct encoding this time
            //   create request
            System.Net.WebRequest objRequest2 = System.Net.HttpWebRequest.Create(sURL);
            //   get response
            System.Net.HttpWebResponse objResponse2;
            objResponse2 = (System.Net.HttpWebResponse)objRequest2.GetResponse();
            //   read response
            using (StreamReader sr = 
              new StreamReader(objResponse2.GetResponseStream(), CorrectEncoding))
            {
                strWebPage = sr.ReadToEnd();
                // Close and clean up the StreamReader
                sr.Close();
            }
        }
    }

关于c# - HttpWebResponse 的编码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/227575/

相关文章:

c# - .NET 2.0 是 .NET 1.1 的彻底改变?

c# - 拆分 IObservable<byte[]> 到字符然后到行

php - 使用 PHP mcrypt 插入时查询随机失败

perl - MediaWiki API 部分名称编码

python - 通过 python 来自 mariadb 的错误文本

c# - 是否可以在单个线程上交错执行多个委托(delegate)?

c# - HttpActionContext.Request 没有 CreateResponse 方法

c# - HttpWebRequest 未传递凭据

python - Networkx 中的编码

python - 这个字符串标准化器 Python 片段有什么问题?