Search code examples
vb.netstringprotx

Read key/value pairs in HttpWebResponse


I want to read a string that looks exactly like this:

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

...into a Dictionary preferably. I've tried splitting on the vbcrlf (there is one on every line I can assure you) and on the '=', but can't quite get it to behave correctly often getting out of bounds errors. Anyone got some fantastic ideas on how to solve this?

Help, as always, appreciated.


Solution

  • You could use a 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