-4

我想从一个站点获取特殊数据,但我不知道如何。

我需要一个 vb.net 源代码来帮助每 5 秒从网络获取这些数据并将它们拆分为一个表单。

我怎样才能做到这一点?

4

1 回答 1

1

看起来网页上的数据被压缩了,所以如果您一直试图通过WebClient.DownloadString, 或WebClient.DownloadData,获取数据System.Text.Encoding.UTF8.GetString,那么您将无法获得可读的字符串。字符串可能看起来像这样

?y?] ?u??j?7? s?H4??N???{?vS(????7?N?±A?O?f??E???-?O??q)?m,:K?:?{Ij .??J?Uem??-K?ni=KT???c?'?g??-??]??A???a?>???o ???????Ys? ?>??????5ga??Z[?v??s?F????i?eU?/?+,??!?f?9? t?2;bG???(??Y!??oX??Gm??W???????Z???8????=?y?WU??9??7; z?^??????T??Y?8]bih??|N? ...

可以看到响应的编码被压缩成如下

client.ResponseHeaders(HttpResponseHeader.ContentEncoding)
' equals gzip

这样做很简单

client.DownloadString(address)

将导致压缩字符串。要考虑压缩,请使用System.IO.GZipStream解压缩。

这段代码是完整的。您所做的就是创建一个新表单,添加一个新标签,然后粘贴这个

Public Class Form1

    Private ReadOnly interval As Integer = 5000
    Private ReadOnly t As New System.Threading.Timer(AddressOf updateLabel, Nothing, -1, -1)
    Private ReadOnly address As String = "http://www.tsetmc.com/tsev2/data/instinfodata.aspx?i=35425587644337450&c=27+"

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        t.Change(0, -1)
    End Sub

    Private Sub updateLabel(state As Object)
        Dim text As String
        Try
            text = getString(address)
            t.Change(interval, -1)
        Catch
            text = "error"
        End Try
        Label1.Invoke(Sub() Label1.Text = text)
    End Sub

    Private Shared Function getString(address As String) As String
        Dim text As String
        Using client As New Net.WebClient()
            Using stream = client.OpenRead(address)
                If client.ResponseHeaders(Net.HttpResponseHeader.ContentEncoding) = "gzip" Then
                    Using responseStream = New IO.Compression.GZipStream(stream, IO.Compression.CompressionMode.Decompress)
                        Using reader = New IO.StreamReader(responseStream)
                            text = reader.ReadToEnd()
                        End Using
                    End Using
                Else
                    Using reader = New IO.StreamReader(stream)
                        text = reader.ReadToEnd()
                    End Using
                End If
            End Using
        End Using
        Return text
    End Function

End Class

上面的代码允许压缩或非压缩响应。如果你知道它总是被压缩的,你可以使用它

Private Shared Function getString(address As String) As String
    Dim text As String
    Using client As New Net.WebClient()
        Using stream = client.OpenRead(address)
            Using responseStream = New IO.Compression.GZipStream(stream, IO.Compression.CompressionMode.Decompress)
                Using reader = New IO.StreamReader(responseStream)
                    text = reader.ReadToEnd()
                End Using
            End Using
        End Using
    End Using
    Return text
End Function

你现在应该得到一个可读的字符串

12:29:37,A ,5254,5218,5203,5223,5277,5190,1727,16938744,88393224291,1,20191125,122937;98/9/4 14:31:01,F,308477.15,2703.04 0.8% ,11256322598042802,2998595530,15357456521865,388202,F,986363991,12363278355103,190712,F,606761,179231120000,6176,;4@63890@5230@5254@100000@2,4@120000@5228@5255@6500@1, 2@40000@5222@5259@221500@6,;65589,406538,760335;10352685,6586059,0,14312016,2626728,573,19,0,456,12;;;0;

于 2019-11-25T21:36:44.617 回答