Search code examples
vb.netresponsewebrequest

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


I send a webrequest to a website with following code.

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+")

but the result is not readable string like bellow:

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�" & .......................

i tried other http web request methods and encodings but the result is the same.


Solution

  • As suggested the problem is the response Encoding that is Gzip. You can see it using a tool like Fiddler for example.

    You can achieve what you want using HttpWebRequest class instead of WebClient and setting AutomaticDecompression property:

        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