ADO - 解析字符串URL并使用参数

时间:2013-12-09 15:01:15

标签: vb.net string parsing ado

我正在将 SQL 中的表格行读入 VB.net webform ,其中值包含网址 字符串

示例:

  

/TEST/Receiver.ashx?input=3&HLR=&sessionID=138&cellid=&datetime=2013120916545057&isnewrequest=0

我正在使用 SqlDataReader

读取所有值

我想接受此字符串并将其解析为:输入 sessionID cellid datetime isnewrequest

然后显示参数及其

知道怎么做吗?

我尝试了一切。

3 个答案:

答案 0 :(得分:1)

只需拆分字符串,然后迭代它, 首先,你必须将它拆分为'?'对于查询参数,然后将其拆分为'&',

迭代你拥有的所有对,并将它们拆分为'='并将键,值添加到List:)

Dim myString As String = "/TEST/Receiver.ashx?input=3&HLR=&sessionID=138&cellid=&datetime=2013120916545057&isnewrequest=0"
Dim pairs As New List(Of KeyValuePair(Of String, String))()
Dim keyValuePairs() As String = myString.Split("?"c)(1).Split("&"c)
For Each pair As String In keyValuePairs
    Dim kp() As String = pair.Split("="c)
    pairs.Add(New KeyValuePair(Of String, String)(kp(0), kp(1)))
Next pair

答案 1 :(得分:0)

如果您有一个有效的URL,您可以使用HttpUtility类的内置功能对其进行解析,如下所示:

Dim myUri As New Uri(urlString)
Dim input As String = HttpUtility.ParseQueryString(myUri.Query).Get("input")

但是,您发布的示例不是有效的URL,因此会引发异常。要修复它,您需要为字符串添加有效的前缀,如下所示:

Dim partialUrlFromDb As String = "/TEST/Receiver.ashx?input=3&HLR=&sessionID=138&cellid=&datetime=2013120916545057&isnewrequest=0"
Dim baseUri As New Uri("http://myfakesite")
Dim myUri As New Uri(baseUri, partialUrlFromDb)
Dim input As String = HttpUtility.ParseQueryString(myUri.Query).Get("input")

答案 2 :(得分:0)

它可能不是最好的方式,但q1,q2,q3,q4为您提供输入,sessionID,cellid,datetime,isnewrequest

 s = "/TEST/Receiver.ashx?input=3&HLR=&sessionID=138&cellid=&datetime=2013120916545057&isnewrequest=0 " ''''''''''''''''''''''''''''

    s1 = InStr(s, "=")

    s2 = InStr(s, "=&")

    q1 = Mid(s, s1 + 1, (s2 - s1) - 1) 'Input

    f1 = Mid(s, s2 + 1) ''''''''''''''''''''''''

    s1 = InStr(f1, "=")

    s2 = InStr(f1, "=&")

    q2 = Mid(f1, s1 + 1, (s2 - s1) - 1) 'Session Id

    f2 = Mid(f1, s2 + 2) '''''''''''''''''''''''''''''''

    s1 = InStr(f2, "=")

    s2 = InStr(f2, "&")

    q3 = Mid(f2, s1 + 1, (s2 - s1) - 1) 'Dattime

    f3 = Mid(f2, s2 + 1)

    s1 = InStr(f3, "=") '''''''''''''''''''''''''''''''

    q4 = Mid(s, s1 + 1) 'isnewrequest