0
我在VB.NET中使用postreq()
發佈了一個web表單。從PostRequest中提取數字
的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
你有正則表達式應該工作;具體是哪裏出了問題? (或者是你使用'Regex'對象的問題?) – Ryan 2013-04-20 04:39:40
是的,但是我只想格式化整數部分。 我想做的事情就像「grep -oE」([0-9] {4})'File'一樣。 – devoidfeast 2013-04-20 04:55:51