0

我正在使用postreq()VB.NET 发布一个 Web 表单。

Response包含一个字符串,,其中是4 位数字。我怎样才能提取这些数字?"sessionId=WXYZ"WXYZ

这是我的代码:

Public Sub btn1_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
    Dim postData As String = "some Post Data"
    Dim url as String = "http://localhost/form.php"
    Dim tempCookies As New CookieContainer
    Dim encoding As New UTF8Encoding
    Dim byteData As Byte() = encoding.GetBytes(postData)
    Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
    postReq.Method = "POST"
    postReq.KeepAlive = True
    postReq.CookieContainer = tempCookies
    postReq.ContentType = "application/x-www-form-urlencoded"
    postReq.Referer = url
    postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
    postReq.ContentLength = byteData.Length

    Dim postreqstream As Stream = postReq.GetRequestStream()
    postreqstream.Write(byteData, 0, byteData.Length)
    postreqstream.Close()
    Dim postresponse As HttpWebResponse

    postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
    tempCookies.Add(postresponse.Cookies)
    logincookie = tempCookies
    Dim postreqreader As New StreamReader(postresponse.GetResponseStream())

    Dim thepage As String = postreqreader.ReadToEnd
    Dim r As New System.Text.RegularExpressions.Regex("sessionId=....")    
End Sub
4

1 回答 1

1

只需将该部分放在一个组中,这样您就可以使用匹配对象的Groups属性来获取它的值:

Dim r As New Regex("sessionId=(\d{4})")
Dim id As String = r.Match(thepage).Groups(1).Value
于 2013-04-20T05:13:44.773 回答