FlewInTheSky 2015-08-13 07:29 采纳率: 100%
浏览 2115
已采纳

[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
  • 写回答

3条回答 默认 最新

  • FlewInTheSky 2015-08-14 09:29
    关注

    发现问题了:

     reader = New StreamReader(Response.GetResponseStream())
    

    改为

     reader = New StreamReader(Response.GetResponseStream(), Encoding.GetEncoding("GB2312"))
    
    

    然后下面输出文件改成Unicode编码就行了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化