我想用這段代碼登錄獲取cookie,然後再訪問用戶頁面,這一部分工作正常。可是如果遇到GB2312編碼的網頁就會出現亂碼。請高手指點,萬分感謝!
另:選這個網址僅供說明只用,沒有別的意思。
Imports System.Net
Imports System.IO
Imports System.Text
Public Class Form1
Const ConnectURL = "http://www.guqu.net/User/User_ChkLogin.asp"
Const HomeURL = "http://www.guqu.net/"
Private Function RegularPage(ByVal URL As String, ByVal CookieJar As CookieContainer) As String
Dim reader As StreamReader
Dim Request As HttpWebRequest = HttpWebRequest.Create(URL)
Request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14"
Request.AllowAutoRedirect = False
Request.CookieContainer = CookieJar
Dim Response As HttpWebResponse = Request.GetResponse()
reader = New StreamReader(Response.GetResponseStream())
Return reader.ReadToEnd()
Response.Close()
End Function
Private Sub LogonPage(ByVal URL As String, ByRef CookieJar As CookieContainer, ByVal PostData As String)
Dim reader As StreamReader
Dim Request As HttpWebRequest = HttpWebRequest.Create(URL)
Request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14"
Request.CookieContainer = CookieJar
Request.AllowAutoRedirect = False
Request.ContentType = "application/x-www-form-urlencoded"
Request.Method = "POST"
Request.ContentLength = PostData.Length
Dim requestStream As Stream = Request.GetRequestStream()
Dim postBytes As Byte() = Encoding.ASCII.GetBytes(PostData)
requestStream.Write(postBytes, 0, postBytes.Length)
requestStream.Close()
Dim Response As HttpWebResponse = Request.GetResponse()
For Each tempCookie In Response.Cookies
CookieJar.Add(tempCookie)
Next
reader = New StreamReader(Response.GetResponseStream())
Response.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim CookieJar As New CookieContainer
Dim PostData As String
Try
'Logon
PostData = "UserName=abc&UserPassword=abcdef&CheckCode=173872&ComeUrl=http%3A%2F%2Fwww.guqu.net%2F&Login.x=30&Login.y=13&Login=+%B5%C7+%C2%BC+"
LogonPage(ConnectURL, CookieJar, PostData)
'Homepage
Dim Sw As StreamWriter = File.CreateText("E:\response.html")
Sw.Write(RegularPage(HomeURL, CookieJar))
Sw.Close()
RichTextBox1.AppendText(RegularPage(HomeURL, CookieJar))
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
End Sub
End Class
發現問題了:
reader = New StreamReader(Response.GetResponseStream())
改為
reader = New StreamReader(Response.GetResponseStream(), Encoding.GetEncoding("GB2312"))
然後下面輸出文件改成Unicode編碼就行了