Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27513

VS 2010 Log In Cookies not Saving/Holding/Working

$
0
0
So I am using ident's CodeBank submission as an example for the project I am working on.

I log into a website using HttpWebRequest and I save the cookies into a CookieContainer after log in, I go to open up a new page inside of the website using the same cookies and it tells me that I am not logged in?

I have been up for 12 straight hours trying to get this to work and have tried almost everything and googled almost everything, the only thing keeping me going is ident's examples...


Here is what I have

Log in sub:
vb .net Code:
  1. Public Sub LoginWebClient()
  2.         cookies = New CookieContainer
  3.         If securityToken = Nothing Then
  4.             securityToken = "guest"
  5.         End If
  6.         lbConsole.Items.Add("Requesting Connection..")
  7.         Dim loginUri As Uri = GetAbsoluteUri(loginHref, tsiUrl)
  8.         Dim loginRequest As HttpWebRequest = TryCast(HttpWebRequest.Create(loginUri), HttpWebRequest)
  9.         If loginRequest Is Nothing Then
  10.             lbConsole.Items.Add("Failed to Create HttpRequest: " + loginUri.ToString)
  11.         End If
  12.         Dim loginPostDataFormatted As String = String.Format(loginPostData,
  13.                                                              HttpUtility.HtmlEncode("username"),
  14.                                                              HttpUtility.HtmlEncode("password"))
  15.         lbConsole.Items.Add("Attempting to log in..")
  16.         Dim loginPostDataBuffer As Byte() = Encoding.ASCII.GetBytes(loginPostDataFormatted)
  17.         loginRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36"
  18.         loginRequest.Method = "post"
  19.         loginRequest.ContentType = "application/x-www-form-urlencoded"
  20.         loginRequest.ContentLength = loginPostDataBuffer.Length
  21.         loginRequest.CookieContainer = cookies
  22.         'loginRequest.Connection = "keep-alive"
  23.  
  24.         Using requestStream As Stream = loginRequest.GetRequestStream
  25.             requestStream.Write(loginPostDataBuffer, 0, loginPostDataBuffer.Length)
  26.             requestStream.Close()
  27.         End Using
  28.  
  29.         lbConsole.Items.Add("Requesting response..")
  30.         Dim loginResponse As HttpWebResponse
  31.  
  32.         Try
  33.             loginResponse = TryCast(loginRequest.GetResponse(), HttpWebResponse)
  34.         Catch wex As WebException
  35.             lbConsole.Items.Add(wex.ToString)
  36.             Exit Sub
  37.         End Try
  38.         lbConsole.Items.Add("Logged in successful.")
  39.         cookies.Add(loginResponse.Cookies)
  40.     End Sub

Attempt at going to next page:
vb .net Code:
  1. Public Function GetPage(ByVal ordnmb As String) As HtmlDocument
  2.         Dim getUri As Uri = GetAbsoluteUri(orderHref + ordnmb, tsiUrl)
  3.         If getUri Is Nothing Then
  4.             lbConsole.Items.Add("Failed to get Order URI")
  5.             Return Nothing
  6.         End If
  7.         lbConsole.Items.Add("Requesting order page..")
  8.         Dim getRequest As HttpWebRequest = TryCast(HttpWebRequest.Create(getUri), HttpWebRequest)
  9.         If getRequest Is Nothing Then
  10.             lbConsole.Items.Add("Failed to create WebRequest for Order Page")
  11.             Return Nothing
  12.         End If
  13.  
  14.         getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36"
  15.         getRequest.ContentType = "application/x-www-form-urlencoded"
  16.         getRequest.CookieContainer = cookies
  17.  
  18.         lbConsole.Items.Add("Requesting response..")
  19.         Dim getResponse As HttpWebResponse
  20.  
  21.         Try
  22.             getResponse = TryCast(getRequest.GetResponse(), HttpWebResponse)
  23.         Catch wex As Exception
  24.             lbConsole.Items.Add("Failed to get response..")
  25.             Return Nothing
  26.         End Try
  27.  
  28.         lbConsole.Items.Add("Reading Response..")
  29.         Dim getSource As String
  30.  
  31.         Using responseStream As Stream = getResponse.GetResponseStream
  32.             Using responsereader As New StreamReader(responseStream)
  33.                 getSource = responsereader.ReadToEnd
  34.             End Using
  35.         End Using
  36.  
  37.         Dim doc As New HtmlDocument
  38.         doc.Load(getSource)
  39.         Return doc
  40.     End Function

Variables:
Code:

    Dim loginHref As String = "usersignin.aspx"
    Dim orderHref As String = "vendor/Tax/OrderDetails.aspx?oid="
    Dim queueHref As String = "vendor/Tax/queue.aspx"
    Dim loginPostData As String = "ctl00%24contentPlaceHolder%24txtUserName={0}&ctl00%24contentPlaceHolder%24txtPassword={1}"
    Private Shared cookies As CookieContainer


Viewing all articles
Browse latest Browse all 27513

Trending Articles