CSDN博客

img AntingZ

如何获取中文网页内容?

发表于2004/6/26 16:14:00  1399人阅读

分类: study road

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才可以正确显示中文。

不知道这是不是规律?

0 0

相关博文

我的热门文章

img
取 消
img