vb.net 中的 Web 请求的响应字符串不可读

I have a problem with not readable response string from a web request in vb.net

我使用以下代码向网站发送网络请求。

Dim webClient As New System.Net.WebClient
webClient.Encoding = Encoding.UTF8
Dim result As String = webClient.DownloadString("http://www.tsetmc.com/tsev2/data/instinfodata.aspx?i=9340069258093236&c=42+")

但结果是不可读的字符串,如下所示:

ChrW(31) & "�" & vbBack & vbNullChar & "w�J]" & vbNullChar & "�u�Y�" & ChrW(28) & "E" & ChrW(16) & "��2/� %�=�m��" & vbFormFeed & "�" & vbNullChar & "�" & vbVerticalTab & ChrW(18) & "�b���E" & ChrW(15) & ChrW(18) & "F2^b���2#�-G��" & ChrW(7) & "=�Ǘ%����" & vbBack & "q~��" & ChrW(31) & "{ۿ�lo�" & vbFormFeed & "]fn�̏�>�g�[��]K�'����x/�""l�" & .......................

我尝试了其他的http web请求方法和编码,但结果是一样的。

正如所建议的那样,问题是 Gzip 的响应编码。 例如,您可以使用 Fiddler 等工具查看它。

您可以使用 HttpWebRequest class 而不是 WebClient 并设置 AutomaticDecompression 属性 来实现您想要的效果:

    Dim req As HttpWebRequest = WebRequest.Create("http://www.tsetmc.com/tsev2/data/instinfodata.aspx?i=9340069258093236&c=42+")
    req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
    req.KeepAlive = True
    req.AutomaticDecompression = DecompressionMethods.GZip
    Dim result As String
    Using response As HttpWebResponse = req.GetResponse()
        Using respStream As IO.Stream = response.GetResponseStream()
            Using sReader As IO.StreamReader = New IO.StreamReader(respStream)
                result = sReader.ReadToEnd()
            End Using
        End Using
    End Using