webclient下载的网页和浏览器显示页面之间的区别

时间:2015-07-07 13:29:07

标签: vb.net

我试图在vb.net下载此网页:

http://www.tsetmc.com/tsev2/data/instinfodata.aspx?i=35425587644337450&c=27

使用这些源代码:

Using client As New WebClient
        Dim arr() As Byte = client.DownloadData("http://www.tsetmc.com/tsev2/data/instinfodata.aspx?i=35425587644337450&c=27")
        MessageBox.Show(System.Text.Encoding.UTF8.GetString(arr))
    End Using

Dim request As WebRequest = WebRequest.Create("http://www.tsetmc.com/tsev2/data/instinfodata.aspx?i=35425587644337450&c=27")
    Using response As WebResponse = request.GetResponse()
        Using reader As New StreamReader(response.GetResponseStream())
            Dim html As String = reader.ReadToEnd()
            File.WriteAllText("test.html", html)
        End Using
    End Using

但结果是:

S�,��e5#;�W�I����^#�+���OO�l�@����5���|�`G��

什么问题? 感谢

1 个答案:

答案 0 :(得分:1)

而不是

File.WriteAllText("test.html", html)

使用

File.WriteAllBytes("test.html", html)

<强>更新

来自网络服务的响应是GZipped。您需要解压缩文本。尝试类似以下内容......

Dim wreq As System.Net.HttpWebRequest = System.Net.WebRequest.Create("http://www.tsetmc.com/tsev2/data/instinfodata.aspx?i=35425587644337450&c=27")
wreq.AutomaticDecompression = Net.DecompressionMethods.GZip

Dim wres As System.Net.WebResponse = wreq.GetResponse
Dim s As System.IO.Stream = wres.GetResponseStream
Dim sr As New System.IO.StreamReader(s)
Dim html As String = sr.ReadToEnd