程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> httpwebresponse-[vb.net]HttpWebResponse的編碼問題

httpwebresponse-[vb.net]HttpWebResponse的編碼問題

編輯:編程綜合問答
[vb.net]HttpWebResponse的編碼問題——GB2312亂碼

我想用這段代碼登錄獲取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編碼就行了

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved