从.net中的webclient下载站点中的字符串/文件

时间:2015-02-12 07:47:38

标签: vb.net webclient downloadstring

任何人都可以帮我从这个网站下载字符串 我使用此代码但

    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“)

结果不同。

真实数据是数字

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

但结果是这样的      “‹ ŠÜT ÿdë’í,«…ohýˆg­}ÿ÷µyÆdöûuuQà”ÄxD¬Ï³K}æ¿Sûù

2 个答案:

答案 0 :(得分:0)

在调用DownloadString之前,您应首先设置webclient的编码。请尝试使用此代码。

    Dim client As New Net.WebClient
    client.Encoding = Encoding.UTF8
    Dim str As String = client.DownloadString("http://goo.gl/JRvlsm")

答案 1 :(得分:0)

如果你“获得”链接的标题:

 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

您发现数据是gzip压缩的(请参阅“Content-Encoding:”行)。要解决这个问题,请使用以下代码:

    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

结果是未压缩的文本,就像您指出的正确数字集一样:

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

这里是我找到解压缩gzip的信息(更多信息):

Automatically decompress gzip response via WebClient.DownloadData

注意:您可能需要在项目中添加“System.IO.Compression”

的引用