How to pass a cookie to a new Httpwebrequest?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Michiel de Vrie
    New Member
    • Dec 2010
    • 1

    How to pass a cookie to a new Httpwebrequest?

    Hello,

    I am writing a simple httpwebrequest bot for a client, but for some reason i can't succeed in passing a single cookie.
    I'll clarify in steps:
    1. Bot connects and gets cookie
    2. Bot tells the server its active
    3. Bot posts data to server
    4. Bots tells server its disconnecting
    5. Bot disconnects and quits


    As far as i am now, i have the bot connecting and receiving the cookie, but its not passing it on to the next httpwebrequest.
    The code:
    Code:
     Dim postStart As HttpWebRequest
            postStart = DirectCast(WebRequest.Create(startURL), HttpWebRequest)
            postStart.CookieContainer = tempcookies
            postStart.Method = "GET"
            postStart.ContentType = "application/x-www-form-urlencoded"
            postStart.UserAgent = "Mozilla/5.0 (X11; Linux x86_64; rv:2.0b4) Gecko/20100818 Firefox/4.0b4"
    
            Dim startresponse As HttpWebResponse
            startresponse = DirectCast(postStart.GetResponse(), HttpWebResponse)
            tempcookies.Add(startresponse.Cookies)
    
            Dim startreader As New StreamReader(startresponse.GetResponseStream())
            Dim start As String
            start = startreader.ReadToEnd
    
            If InStr(start, "Connecting") Then
                TohlaWorker.ReportProgress(0, start)
                GoTo verbinden
            ElseIf InStr(start, "5") Then
                TohlaWorker.ReportProgress(0, "Connected")
                gesprekcookie = tempcookies
    
                Dim postStarttyping As HttpWebRequest
                postStarttyping = DirectCast(WebRequest.Create(typeURL), HttpWebRequest)
                postStarttyping.Method = "POST"
                postStarttyping.ContentType = "application/x-www-form-urlencoded"
                postStarttyping.CookieContainer = gesprekcookie
                postStarttyping.UserAgent = "Mozilla/5.0 (X11; Linux x86_64; rv:2.0b4) Gecko/20100818 Firefox/4.0b4"
    
    
                Dim starttypingresponse As HttpWebResponse
                starttypingresponse = DirectCast(postStarttyping.GetResponse(), HttpWebResponse)
                Dim poststartreader As New StreamReader(starttypingresponse.GetResponseStream())
                Dim reply As String
                reply = poststartreader.ReadToEnd
    
                If InStr(reply, "1") Then
                    TohlaWorker.ReportProgress(0, "Simulating typing..")
                Else
                    TohlaWorker.ReportProgress(0, "Error simulating typing, starting over")
                    GoTo verbinden
                End If
    If i check cook.Value after the second step, its empty. I dont understand whats going on here, can anyone point me in the right direction?
Working...