VB .NET How do I use the cookies to navigate on the site after login ?

Discussion in 'Programming' started by Just need it, Nov 3, 2011.

  1. #1
    Hi there,
    what exactly I want to know is the use of cookies in VB. I am creating a program that login to a website and then let user perform tasks on the site. But I cant make it to navigate on the website, after logging into the website and collecting cookies, when the program navigate to another page the website ask for the login again. How do I use the cookies that I have collected to tell the website that I have just authenticated ?

    here are the codes that I am using (assume all the variables are declared globally)
    .
    .
    .
    Private Sub loginbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles loginbtn.Click
                    dim url as system.uri = new system.uri("http://www.somewebsite.com/login.php")
                    request = WebRequest.Create(url)
                    loginid.Enabled = False
                    loginpass.Enabled = False
                    loginbtn.Enabled = False
                    Me.Text = "Logging in..."
                    request.Method = "POST"
                    postData = "MobileNoLogin=" + loginid.Text + "&" + "LoginPassword=" + loginpass.Text
                    byteArray = Encoding.UTF8.GetBytes(postData)
                    request.CookieContainer = cookiejar
                    request.ContentLength = byteArray.Length
                    request.ContentType = "application/x-www-form-urlencoded"
                    request.KeepAlive = True
                    dataStream = request.GetRequestStream()
                    dataStream.Write(byteArray, 0, byteArray.Length)
                    response = request.GetResponse()
                    stat = (CType(response, HttpWebResponse).StatusDescription)
                    dataStream = response.GetResponseStream()
                    reader = New StreamReader(dataStream)
                    responseFromServer = reader.ReadToEnd
                    messagebox.show(responsefromserver")                [B] '----------->> Shows that I am logged in :)[/B]
                    reader.Close()
                    dataStream.Flush()
                    dataStream.Close()
                    response.Close()
    End Sub
    
    
    Private Sub sendbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sendbtn.Click
    
                    ' HERE IT REDIRECT ME TO THE LOGIN PAGE AGAIN :(
            
                    request = WebRequest.Create("http://www.website.com/submitmessage.php")
                    request.Method = "POST"
                    postData = "recepient=somerecepient&Message=Hi, I am testing my application to send messages"
                    byteArray = Encoding.UTF8.GetBytes(postData)
                    request.CookieContainer = cookiejar
                    request.ContentLength = byteArray.Length
                    request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.11) Gecko/20101012 Firefox/3.6.11"
                    request.ContentType = "application/x-www-form-urlencoded"
                    dataStream = request.GetRequestStream()
                    dataStream.Write(byteArray, 0, byteArray.Length)
                    response = request.GetResponse()
                    dataStream.Close()
                    stat = (CType(response, HttpWebResponse).StatusDescription)
                    dataStream = response.GetResponseStream()
                    reader = New StreamReader(dataStream)
                    responseFromServer = reader.ReadToEnd
                    messagebox.show(responsefromserver)             [B]'--------------> Here I can see the redirection to the login page :([/B]
                Catch ex As Exception
                    If ex.ToString.Length <> 0 Then
                        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, False)
                        Exit Try
                    End If
                End Try
            End If
        End Sub
    Code (markup):


    Can anybody tell me what I am doing wrong here ? I have the login cookies after successful login but still I cant browse to another page, Please can anybody tell me the right way ?
     
    Just need it, Nov 3, 2011 IP
  2. Just need it

    Just need it Active Member

    Messages:
    146
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #2
    anybody ??
    someone please help.
     
    Just need it, Nov 4, 2011 IP