有人能帮我从这个网站下载字符串吗我使用这个代码,但
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")
结果是不同的。
真正的数据是数字
"20081206924991689249917885396249178;200812079178913091789130117523539130"
但结果是"‹ ŠÜT ÿdë’í,«…ohýˆg}ÿ÷µyÆdöûuuQà”ÄxD¬Ï³K}æ¿Sûù
"
在调用DownloadString之前,应该先设置web客户端的编码。请尝试使用以下代码。
Dim client As New Net.WebClient
client.Encoding = Encoding.UTF8
Dim str As String = client.DownloadString("http://goo.gl/JRvlsm")
如果您"得到"链接的标题:
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压缩的(请参阅"内容编码:"行)。要解决这个问题,请使用以下代码:
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的信息(更多信息):
通过WebClient.DownloadData 自动解压缩gzip响应
注意:您可能需要在项目中添加"System.IO.Compression"的引用