我想用这段代码登录获取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