我在VB.NET中使用postreq()
发布了一个Web表单。
Response
包含一个字符串"sessionId=WXYZ"
,其中 WXYZ
为4位数。我该如何提取这些数字?
这是我的代码:
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
答案 0 :(得分:1)
只需将该部分放入一个组中,这样就可以使用匹配对象的Groups
property获取其值:
Dim r As New Regex("sessionId=(\d{4})")
Dim id As String = r.Match(thepage).Groups(1).Value