302 Redirects

graham23s

Member
Joined
Feb 22, 2009
Messages
13
Programming Experience
1-3
Hi Guys,

I have been at this for a while now and can't seem to locate the problem, i'm doing an httpwebrequest to login to a site:

code:

VB.NET:
            Try

                'Make a request for the desired web page
                Dim POSTRequest As HttpWebRequest = CType(WebRequest.Create("https://secure.site.com/login"), HttpWebRequest)

                'Get an empty cookie jar ready
                Dim CookieJar As New CookieContainer()

                'Set the POST data to be sent
                Dim POSTData As String = "POST DATA HERE"

                'Encode the POST data into bytes
                Dim POSTBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(POSTData)

                'Set other options
                With POSTRequest
                    .Method = "POST"
                    .UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)"
                    .KeepAlive = True
                    .AllowAutoRedirect = False
                    .CookieContainer = CookieJar
                    .ContentType = "application/x-www-form-urlencoded"
                    .ContentLength = POSTBytes.Length
                    .CookieContainer = CookieJar
                End With

                'Set the data stream to be read
                Dim POSTDatastream = POSTRequest.GetRequestStream()

                'Send/write the data
                POSTDatastream.Write(POSTBytes, 0, POSTBytes.Length)

                'Get the response that contains any cookies
                Dim POSTResponse As HttpWebResponse = CType(POSTRequest.GetResponse(), HttpWebResponse)
                'MessageBox.Show(POSTResponse.StatusCode)
                'Set the stream reader
                Dim POSTStreamreader As New System.IO.StreamReader(POSTResponse.GetResponseStream)

                'Add cookies to the cookie jar
                CookieJar.Add(POSTResponse.Cookies)

                'Debugging
                Dim POSTHTMLsource As String = Trim(POSTStreamreader.ReadToEnd())

                'Output the html for debugging purposes
                debugTextBox.Text = POSTHTMLsource

                'Close the streamreader and the request
                POSTStreamreader.Close()
                POSTResponse.Close()

                'Make a request for the desired web page
                Dim GETRequest As HttpWebRequest = CType(WebRequest.Create("http://site.com/graham29s"), HttpWebRequest)

                'Set other details
                With GETRequest
                    .Method = "GET"
                    .KeepAlive = True
                    .UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)"
                    .ContentType = "application/x-www-form-urlencoded"
                    .CookieContainer = CookieJar
                End With

                'Get the response from the server
                Dim GETResponse As HttpWebResponse = CType(GETRequest.GetResponse(), HttpWebResponse)
                'MessageBox.Show(GETResponse.StatusCode)
                'Add cookies to the cookie jar
                CookieJar.Add(GETResponse.Cookies)

                'Get the Page sourcecode and put it in a variable
                Dim GETStreamreader As New System.IO.StreamReader(GETResponse.GetResponseStream)

                'The html in a variable
                Dim GETHTMLSource As String = GETStreamreader.ReadToEnd()

                'Output the html for debugging purposes
                debugTextBox.Text = GETHTMLSource

                'Close down the first request FINISHED!
                GETResponse.Close()
                GETStreamreader.Close()

                For Each C As Cookie In GETResponse.Cookies
                    MessageBox.Show(String.Format("{0}={1}", C.Name, C.Value))
                Next

            Catch ex As Exception

                MessageBox.Show(ex.ToString)

            End Try

The initial login is fine, but when i look at the header data sent using livehttp i see:

VB.NET:
HTTP/1.x 302 Found
Date: Sat, 21 Mar 2009 20:38:13 GMT
P3P: policyref="http://p3p.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV"
Set-Cookie: _user=graham29s%202.1%201237667893%20-1%20%2043fddde6e5c5525a161288c95f445c3d%20XLjaklJ_HM3ewV0oEmOeM5uzTwM-; path=/; domain=.site.com
Set-Cookie: site_us_production=sUlszDQ0fCKzSMVDebKSOCTWydI0u9d.O_5k0KI6fSBa9RpLhe.D4MgMBeQpRTSX_B7XIo6W9RIJBMIwQPPkn53S9xFJnOyQ4Q2ejyG0NlhOsiuCgONHu.C0OW9iiIhQGc50O6XxnxsU2xHu3ilqgpCRME6VqPlQazvC9E9QtSvBHGGZvotjc1xAtVCfq8KOk7RjB1SF_.w5ux0q8H_XWCbDq2Kx8QgZSeYsPr61Cv5o9ygnd6fUMadp6FyLZpXxLlHjMQ21AA8JCfAEjt7tCUtPMQchpy4rtiSymbejrr2gMq1ikcX7qobmeHgypqEu9H5Bi79eZwVymMvOWa1OWZkfARAhqEpYwzh609WT; expires=Sun, 21-Mar-2010 20:38:13 GMT; path=/; domain=.site.com
Location: http://site.com/graham29s
Cache-Control: private
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8

when i loop the cookies i am missing one:

Set-Cookie: _user=graham29s%202.1%201237667893%20-1%20%2043fddde6e5c5525a161288c95f445c3d%20XLjaklJ_HM3ewV0oEmOeM5uzTwM-; path=/; domain=.site.com

i only get this one:

Set-Cookie: site_us_production=sUlszDQ0fCKzSMVDebKSOCTWydI0u9d.O_5k0KI6fSBa9RpLhe.D4MgMBeQpRTSX_B7XIo6W9RIJBMIwQPPkn53S9xFJnOyQ4Q2ejyG0NlhOsiuCgONHu.C0OW9iiIhQGc50O6XxnxsU2xHu3ilqgpCRME6VqPlQazvC9E9QtSvBHGGZvotjc1xAtVCfq8KOk7RjB1SF_.w5ux0q8H_XWCbDq2Kx8QgZSeYsPr61Cv5o9ygnd6fUMadp6FyLZpXxLlHjMQ21AA8JCfAEjt7tCUtPMQchpy4rtiSymbejrr2gMq1ikcX7qobmeHgypqEu9H5Bi79eZwVymMvOWa1OWZkfARAhqEpYwzh609WT; expires=Sun, 21-Mar-2010 20:38:13 GMT; path=/; domain=.site.com

Do i need to handle the 302 redirect differently at all, that a normal request?

thanks for any help guys

Graham
 
Back
Top