内部循环中的Webrequest仅适用于第一遍

时间:2015-03-26 14:47:53

标签: c# .net vb.net httpwebrequest

我在每个循环中执行以下代码,它对列表框中的每个项执行。但是,在第一次执行后,下一个(POST)请求似乎由于某种原因不起作用。 我使用fiddler进行了调试,但状态代码还可以,令牌(RTVT& reloadToken)也是新的。 我在stackoverflow上找到了以下已经提出的问题:Executing web requests inside of a loop only works on the first pass 但我完全不明白是什么修正了他的错误。

这是我当前在循环中执行的代码:

'Get reloadToken and RTVT
        Dim tokenRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create("http://" & server & ".seafight.bigpoint.com/index.es?action=internalHome"), HttpWebRequest)
        tokenRequest.CookieContainer = cookieContainer
        tokenRequest.Method = "GET"
        tokenRequest.KeepAlive = True
        tokenRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36"

        Dim marketSourceCode As String = String.Empty
        Using sfreader As New StreamReader(tokenRequest.GetResponse().GetResponseStream())
            marketSourceCode = sfreader.ReadToEnd()
        End Using
        'WebBrowser1.DocumentText = marketSourceCode

        Dim reloadToken As String = RegularExpressions.Regex.Match(marketSourceCode, "(?<=reloadToken"" value="")(.*)(?="" />)").Groups(1).Value
        Dim RTVT As String = RegularExpressions.Regex.Match(marketSourceCode, "(?:RTVT.+value=')(?<RTVT>[^']+)").Groups("RTVT").Value

        Dim bidPostData As String = String.Format("reloadToken=" & reloadToken & "&setBid=true&ItemID=" & itemID & "&ItemType=" & itemType & "&currentSelecter=all&bidAmount=" & amount & "&RTVT=" & RTVT & "&ajaxAction=biddOnItem")

        Dim bidRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create("http://" & server & ".seafight.bigpoint.com/ajax.es"), HttpWebRequest)
        bidRequest.CookieContainer = cookieContainer
        bidRequest.Method = "POST"
        bidRequest.KeepAlive = True
        bidRequest.ContentLength = bidPostData.Length
        bidRequest.ContentType = "application/x-www-form-urlencoded"
        bidRequest.AllowAutoRedirect = True
        bidRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36"
        bidRequest.Referer = "http://" & server & ".seafight.bigpoint.com/index.es?action=internalMarketplace&subact=Elite"

        Dim encoding As New UTF8Encoding()
        Dim bidDataBytes As Byte() = encoding.GetBytes(bidPostData)
        bidRequest.ContentLength = bidDataBytes.Length
        Dim stream As Stream = bidRequest.GetRequestStream()
        stream.Write(bidDataBytes, 0, bidDataBytes.Length)

        Dim webResp As HttpWebResponse = DirectCast(bidRequest.GetResponse(), HttpWebResponse)

        Dim datastream As Stream = webResp.GetResponseStream()
        Dim reader As New StreamReader(datastream)

        Dim bidded As String = reader.ReadToEnd()

        WebBrowser1.DocumentText = bidded

        bidRequest.Abort()
        reader.Close()
        datastream.Close()
        webResp.Close()
        stream.Close()

0 个答案:

没有答案