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:
Attempt at going to next page:
Variables:
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:
Public Sub LoginWebClient() cookies = New CookieContainer If securityToken = Nothing Then securityToken = "guest" End If lbConsole.Items.Add("Requesting Connection..") Dim loginUri As Uri = GetAbsoluteUri(loginHref, tsiUrl) Dim loginRequest As HttpWebRequest = TryCast(HttpWebRequest.Create(loginUri), HttpWebRequest) If loginRequest Is Nothing Then lbConsole.Items.Add("Failed to Create HttpRequest: " + loginUri.ToString) End If Dim loginPostDataFormatted As String = String.Format(loginPostData, HttpUtility.HtmlEncode("username"), HttpUtility.HtmlEncode("password")) lbConsole.Items.Add("Attempting to log in..") Dim loginPostDataBuffer As Byte() = Encoding.ASCII.GetBytes(loginPostDataFormatted) loginRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36" loginRequest.Method = "post" loginRequest.ContentType = "application/x-www-form-urlencoded" loginRequest.ContentLength = loginPostDataBuffer.Length loginRequest.CookieContainer = cookies 'loginRequest.Connection = "keep-alive" Using requestStream As Stream = loginRequest.GetRequestStream requestStream.Write(loginPostDataBuffer, 0, loginPostDataBuffer.Length) requestStream.Close() End Using lbConsole.Items.Add("Requesting response..") Dim loginResponse As HttpWebResponse Try loginResponse = TryCast(loginRequest.GetResponse(), HttpWebResponse) Catch wex As WebException lbConsole.Items.Add(wex.ToString) Exit Sub End Try lbConsole.Items.Add("Logged in successful.") cookies.Add(loginResponse.Cookies) End Sub
Attempt at going to next page:
vb .net Code:
Public Function GetPage(ByVal ordnmb As String) As HtmlDocument Dim getUri As Uri = GetAbsoluteUri(orderHref + ordnmb, tsiUrl) If getUri Is Nothing Then lbConsole.Items.Add("Failed to get Order URI") Return Nothing End If lbConsole.Items.Add("Requesting order page..") Dim getRequest As HttpWebRequest = TryCast(HttpWebRequest.Create(getUri), HttpWebRequest) If getRequest Is Nothing Then lbConsole.Items.Add("Failed to create WebRequest for Order Page") Return Nothing End If getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36" getRequest.ContentType = "application/x-www-form-urlencoded" getRequest.CookieContainer = cookies lbConsole.Items.Add("Requesting response..") Dim getResponse As HttpWebResponse Try getResponse = TryCast(getRequest.GetResponse(), HttpWebResponse) Catch wex As Exception lbConsole.Items.Add("Failed to get response..") Return Nothing End Try lbConsole.Items.Add("Reading Response..") Dim getSource As String Using responseStream As Stream = getResponse.GetResponseStream Using responsereader As New StreamReader(responseStream) getSource = responsereader.ReadToEnd End Using End Using Dim doc As New HtmlDocument doc.Load(getSource) Return doc 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