vb.net StreamReader.ReadToEnd() 使用什么字符编码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13338404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What character encoding is used by StreamReader.ReadToEnd()?
提问by CJ7
- What character encoding is used by
StreamReader.ReadToEnd()? - What would be the reason to use (b) instead of (a) below?
- Is there a risk of their being a character encoding problem if (a) is used instead of (b)?
- Is there another method that is better than (a) and (b)?
- 使用什么字符编码
StreamReader.ReadToEnd()? - 下面使用 (b) 而不是 (a) 的原因是什么?
- 如果使用 (a) 而不是 (b),它们是否有成为字符编码问题的风险?
- 有没有比(a)和(b)更好的方法?
(a)
(一种)
Dim strWebResponse As String
Dim Request As HttpWebRequest = WebRequest.Create(Url)
Using Response As WebResponse = smsRequest.GetResponse()
Using reader As StreamReader = New StreamReader(Response.GetResponseStream())
strWebResponse = reader.ReadToEnd()
End Using
End Using
(b)
(二)
Dim encoding As New UTF8Encoding()
Dim strWebResponse As String
Dim Request As HttpWebRequest = WebRequest.Create(Url)
Using Response As WebResponse = Request.GetResponse()
Dim responseBuffer(Response.ContentLength - 1) As Byte
Response.GetResponseStream().Read(responseBuffer, 0, Response.ContentLength - 1)
strWebResponse = encoding.GetString(responseBuffer)
End Using
回答by Jim Mischel
The standard encoding used by StreamReaderis Encoding.Default, which will vary from machine to machine depending on your version of Windows and the locale that you have set.Encoding.UTF8.
使用的标准编码StreamReader是Encoding.Default,它会因机器而异,具体取决于您的 Windows 版本和您设置的区域设置。Encoding.UTF8.
I have trouble remembering what the defaults are, so I prefer to use the StreamReaderconstructor that lets me specify the encoding. For example:
我很难记住默认值是什么,所以我更喜欢使用StreamReader让我指定编码的构造函数。例如:
Using reader As StreamReader = New StreamReader(Response.GetResponseStream(), Encoding.UTF8)
See the constructor documentationfor more info.
有关更多信息,请参阅构造函数文档。
If you use that constructor in your example a, the results will be the same as for your example b.
如果在示例 a 中使用该构造函数,结果将与示例 b 相同。
Should you use UTF-8? That depends on the page you're downloading. If the page you're downloading was encoded with UTF-8 then, yes, you should use UTF-8. UTF-8 is supposed to be the default if no character set is defined in the HTTP headers. But you need to check the Content-Typeheader to determine if the page uses some other encoding. For example, the Content-Typeheader might read:
你应该使用UTF-8吗?这取决于您下载的页面。如果您下载的页面是用 UTF-8 编码的,那么,是的,您应该使用 UTF-8。如果 HTTP 标头中没有定义字符集,则 UTF-8 应该是默认值。但是您需要检查Content-Type标题以确定页面是否使用其他编码。例如,Content-Type标题可能是:
application/xml; charset=ISO-8859-2
You would have to examine the ContentTypeproperty of the HttpWebResponse, check to see if there is a charsetfield, and set the encoding properly based on that.
您必须检查 的ContentType属性HttpWebResponse,检查是否有charset字段,并根据该字段正确设置编码。
Or, just use UTF-8 and hope for the best.
或者,只使用 UTF-8 并希望最好。
回答by Ravindra Bagale
yes b is good because UTF-8 will work with any ASCII document
UTF8 is Unicode encoding type.
More importantly its backwards compatible with ASCII,& the standard default for XML and HTML
是的 b 很好,因为UTF-8 will work with any ASCII document
UTF8 是Unicode encoding type.
更重要的是它的backwards compatible with ASCII,&standard default for XML and HTML
回答by user3096335
I found a solution, It's not pretty but it works.
First you'll have to set your StreamReader to DetectEncoding as true,
Then you put some special character on your page.
我找到了一个解决方案,它不漂亮,但有效。
首先,您必须将 StreamReader 的 DetectEncoding 设置为 true,
然后在页面上放置一些特殊字符。
StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.Default, true);
<%@ Page Title="FTP - Verifica??o" Language="C#"
StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.Default, true);
<%@ 页面标题="FTP - Verifica??o" Language="C#"

