0

Can anybody help me to download string from this site I use this code but

    Dim client As New Net.WebClient
    Dim str As String = client.DownloadString("

http://www.tsetmc.com/tsev2/chart/data/IndexFinancial.aspx?i=32097828799138957&t=ph")

the results are different.

true data are numbers

"20081206,9249,9168,9249,9178,8539624,9178;20081207,9178,9130,9178,9130,11752353,9130"

but results are like "‹ ŠÜT ÿdë’í,«…ohýˆg­}ÿ÷µyÆdöûuuQà”ÄxD¬Ï³K}æ¿Sûù"

user1670642
  • 91
  • 4
  • 10

2 Answers2

1

You should set the webclient's encoding first before calling DownloadString.Try with this code.

    Dim client As New Net.WebClient
    client.Encoding = Encoding.UTF8
    Dim str As String = client.DownloadString("http://goo.gl/JRvlsm")
akhil kumar
  • 1,598
  • 1
  • 13
  • 26
  • thanks i use your code but results are same "� ��T �d���,��oh��g�}���yƐd��uuQ��xD�ϳK}�S��w�܉��o�vf�"��:��O���+e��F�Y�퉤g?/v��Q�)e��YKdxV ��^�����Ae��o��?��^wY]Tf���(����km�ʬ*2�S���2��)�2뼹��0}�3g|3��Z(�5��E����EdN=0n&a��(k7�Y���ڄ=��Q�s�ʬ" – user1670642 Feb 12 '15 at 09:27
  • couldn't check and see whats the matter.you can view this link [encoding for webclient](http://stackoverflow.com/questions/2700638/characters-in-string-changed-after-downloading-html-from-the-internet) for more help. – akhil kumar Feb 12 '15 at 09:40
  • i dont understand this [code](http://stackoverflow.com/questions/2700638/characters-in-string-changed-after-downloading-html-from-the-internet) can you help me more – user1670642 Feb 12 '15 at 15:04
0

If you "get" the headers for your link:

 Status:200
 Raw:
 HTTP/1.1 200 OK
 Cache-Control: public, max-age=9999
 Content-Length: 33183
 Content-Type: text/csv; charset=utf-8
 Content-Encoding: gzip
 Expires: Sat, 23 Jul 2016 02:32:58 GMT
 Last-Modified: Fri, 22 Jul 2016 23:46:19 GMT
 Vary: *
 Set-Cookie: ASP.NET_SessionId=vsxyok45zvtgsbvp4iqxdh45; path=/; HttpOnly
 X-Powered-By: ASP.NET
 Date: Fri, 22 Jul 2016 23:46:19 GMT

 Request:
 GET /tsev2/chart/data/IndexFinancial.aspx?i=32097828799138957&t=ph HTTP/1.1

You find that the data is gzip compressed (see the "Content-Encoding:" line). To address that, use this code:

    Dim myUrl As String = "http://www.tsetmc.com/tsev2/chart/data/IndexFinancial.aspx?i=32097828799138957&t=ph"
    Dim result as string
    Using client As New WebClient
        client.Headers(HttpRequestHeader.AcceptEncoding) = "gzip"
        Using rs As New GZipStream(client.OpenRead(myUrl), CompressionMode.Decompress)
            result = New StreamReader(rs).ReadToEnd()
        End Using
    End Using

The result is uncompressed text, just as you have indicated as the correct set of numbers:

20081206,9249,9168,9249,9178,8539624,9178;20081207,9178,9130,9178,9130,11752353,9130;

Here is where I found the info for decompressing gzip (more info there):

Automatically decompress gzip response via WebClient.DownloadData

Note: you may have to add a reference in your project for "System.IO.Compression"

Community
  • 1
  • 1
an odder guest
  • 194
  • 2
  • 5