获取中文网页内容的方法技巧
Imports System.Net
Imports System.IO
(1)
??????? Dim req As WebRequest = WebRequest.Create("http://mydotnet.go.nease.net")
??????? Dim resp As WebResponse = req.GetResponse()
??????? Dim s As Stream = resp.GetResponseStream()
??????? Dim sr As StreamReader = New StreamReader(s, System.Text.Encoding.Default)
??????? Dim str As String = sr.ReadToEnd()
??????? TextBox1.Text = str
(2)
??????? Dim req As WebRequest = WebRequest.Create("http://blog.csdn.net/AngintZ")
??????? Dim resp As WebResponse = req.GetResponse()
??????? Dim s As Stream = resp.GetResponseStream()
??????? Dim sr As StreamReader = New StreamReader(s, System.Text.Encoding.UTF8)
??????? Dim str As String = sr.ReadToEnd()
??????? TextBox1.Text = str
如果网页中有中文,不同的网页应该选用不同字符编码才能正确的获得中文.
简单对比了几个网页,发现如果网页的meta里面有charset=gb2312:
charset=gb2312">
用System.Text.Encoding.Default就可以正确显示
如果meta里面没有charset=gb2312的话用System.Text.Encoding.UTF8才可以正确显示中文。
不知道这是不是规律?
本文地址:http://www.45fan.com/a/question/67192.html