读取HttpWebResponse中的键/值对

时间:2010-01-26 16:01:08

标签: vb.net string protx

我想读一个看起来完全像这样的字符串:

VPSProtocol=2.22
Status=OK
StatusDetail=0000 : The Authorisation was Successful.
VPSTxId={BBF09A43-913E-14E3-B41B-E5464B6FF8A9}
SecurityKey=EH8VFZUSH9
TxAuthNo=4979698
AVSCV2=SECURITY CODE MATCH ONLY
AddressResult=NOTMATCHED
PostCodeResult=NOTMATCHED
CV2Result=MATCHED
CAVV=AAABARR5kwAAAAAAAAAAAAAAAAA=
3DSecureStatus=OK

...最好进入字典。我已经尝试拆分vbcrlf(我可以向你保证每一行都有一个)和'=',但是不能让它正常运行,经常出现超出界限的错误。任何人都有一些关于如何解决这个问题的奇妙想法?

一如既往地提供帮助。

2 个答案:

答案 0 :(得分:1)

您可以使用StringReader

Dim dic = New Dictionary(Of String, String)
Using reader = New StringReader(someString)
    Dim line As String = reader.ReadLine()
    While line <> Nothing
        Dim tokens = line.Split("=")
        If tokens.Length < 2 Then
            Continue While
        End If
        dic.Add(tokens(0), tokens(1))
        line = reader.ReadLine()
    End While
End Using

答案 1 :(得分:0)

我用这段相当讨厌的代码解决了这个问题;

    Dim dictionary As New Dictionary(Of String, String)
    Dim x As String = "x?" & Replace(tmp.ToString(), vbCrLf, "&")
    Dim arr As String() = x.Split("?".ToCharArray())
    If arr.Length >= 2 Then
        Dim arr2 As String() = arr(1).Split("&".ToCharArray())
        For Each item As String In arr2
            Dim arr3 As String() = item.Split("=".ToCharArray())
            dictionary.Add(arr3(0), arr3(1))
        Next
    End If

我希望它可以帮助那里的人,如果有人可以建议改进,请执行:)