本文介绍了编码麻烦HttpWebResponse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是code的一个片段:

 的HttpWebRequest的WebRequest =(HttpWebRequest的)WebRequest.Create(request.RawUrl);
WebRequest.DefaultWebProxy = NULL; //确保我们不会在代理又往前走循环
HttpWebResponse响应=(HttpWebResponse)webRequest.GetResponse();
字符串charset = response.CharacterSet;
编码方式进行编码;
如果(String.IsNullOrEmpty(字符集))
编码= Encoding.Default;
其他
编码= Encoding.GetEncoding(字符集);StreamReader的resStream =新的StreamReader(response.GetResponseStream(),编码);
返回resStream.ReadToEnd();

问题是如果我测试:

所有é不显示良好。我试图改变ASCII为UTF8,它仍然显示错误。我已经测试的HTML文件在浏览器和浏览器中显示HTML文本很好,所以我pretty确定问题是我用下载HTML文件的方法。

我应该怎么改?

删除死ImageShack链接

更新1:code和测试文件修改


解决方案

首先,写一个code的更简单的方法是使用一个StreamReader和ReadToEnd的:

 的HttpWebRequest的WebRequest =(HttpWebRequest的)WebRequest.Create(myURL);
使用(HttpWebResponse响应=(HttpWebResponse)webRequest.GetResponse())
{
    使用(流resStream = response.GetResponseStream())
    {
        StreamReader的读者=新的StreamReader(resStream,编码???。);
        返回reader.ReadToEnd();
    }
}

然后,它只是一个找到正确的编码问题。你是如何创建的文件?如果是用记事本,那么你可能想 Encoding.Default - 但是这显然不是便携式的,因为它是默认编码的的PC。

在一个运行良好的Web服务器,响应将显示在其标题中的编码。话虽如此,响应头有时声称一件事和HTML声称另一个,在某些情况下。

Here is a snippet of the code :

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();

The problem is if I test with : http://www.google.fr

All "é" are not displaying well. I have try to change ASCII to UTF8 and it still display wrong. I have tested the html file in a browser and the browser display the html text well so I am pretty sure the problem is in the method I use to download the html file.

What should I change?

removed dead ImageShack link

Update 1: Code and test file changed

解决方案

Firstly, the easier way of writing that code is to use a StreamReader and ReadToEnd:

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(myURL);
using (HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse())
{
    using (Stream resStream = response.GetResponseStream())
    {
        StreamReader reader = new StreamReader(resStream, Encoding.???);
        return reader.ReadToEnd();
    }
}

Then it's "just" a matter of finding the right encoding. How did you create the file? If it's with Notepad then you probably want Encoding.Default - but that's obviously not portable, as it's the default encoding for your PC.

In a well-run web server, the response will indicate the encoding in its headers. Having said that, response headers sometimes claim one thing and the HTML claims another, in some cases.

这篇关于编码麻烦HttpWebResponse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 06:49
查看更多