Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 15th, 2008, 08:20 AM
Registered User
 
Join Date: Sep 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default HTTPWEBRESPONSE and HTTPWEBREQUEST and redirect

Hi All,

I have to post a request to a login page using the userid and password and then get the location and session cookiew from the responsestream, and then in return use that information to redirect the user to the URl that Header 'Location' has.

My question is I can send a get using the responsestream and get the response for the location page but all the images on the site are not showing up. Also If I want to redirect the user to the menu page after login, my session id is lost. I am attaching the code below for both - if someone has a solution to this it will be much appreciated.

Dim dByte() As Byte = enc.GetBytes(sFormdata)

        Try

            Dim req As HttpWebRequest = HttpWebRequest.Create("https://somesite.com/login")
            With req
                .KeepAlive = False
                .Method = "POST"
                .AllowAutoRedirect = False
                .ContentType = "application/x-www-form-urlencoded"
                .ContentLength = dByte.Length
                .CookieContainer = cookieC
            End With

            'add the POST data
            Dim sendREQ As IO.Stream = req.GetRequestStream()
            sendREQ.Write(dByte, 0, dByte.Length)
            sendREQ.Close()

            req.Credentials = CredentialCache.DefaultCredentials
            'Obtain the response

            Dim Res As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
            Dim sr As StreamReader = New StreamReader(Res.GetResponseStream(), Text.Encoding.UTF8)
            Dim strData As String = sr.ReadToEnd

            'Add the headers
            Dim redirect As String = Res.Headers("Location")

            'add cookie
            cookieC.Add(Res.Cookies)


            'Move to redirect page as get request
            req = WebRequest.Create(redirect)

            With req
                .KeepAlive = False
                .Method = "GET"
                .ContentType = "application/x-www-form-urlencoded"
                .AllowAutoRedirect = False
                .CookieContainer = cookieC
            End With

            'get the page
            Res = req.GetResponse
            Dim sReader As IO.StreamReader = New IO.StreamReader(Res.GetResponseStream)
            Dim html As String = sReader.ReadToEnd
            Response.Write(html)


for the redirect - I was trying to use the code below and instead of going to the menu page I was thrown back to the login page
Dim dByte() As Byte = enc.GetBytes(sFormdata)

        Try

            Dim req As HttpWebRequest = HttpWebRequest.Create("https://somesite.com/login")
            With req
                .KeepAlive = False
                .Method = "POST"
                .AllowAutoRedirect = False
                .ContentType = "application/x-www-form-urlencoded"
                .ContentLength = dByte.Length
                .CookieContainer = cookieC
            End With

            'add the POST data
            Dim sendREQ As IO.Stream = req.GetRequestStream()
            sendREQ.Write(dByte, 0, dByte.Length)
            sendREQ.Close()

            req.Credentials = CredentialCache.DefaultCredentials
            'Obtain the response

            Dim Res As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
            Dim sr As StreamReader = New StreamReader(Res.GetResponseStream(), Text.Encoding.UTF8)
            Dim strData As String = sr.ReadToEnd

            'Add the headers
            Dim redirect As String = Res.Headers("Location")

            'add cookie
            cookieC.Add(Res.Cookies)




            Dim rs As HttpResponse = Context.Response
            Dim hCookie As New HttpCookie("CHUBB")
            hCookie.Name = "Cookie"
            hCookie.Value = Res.Cookies(0).Name & "=" & Res.Cookies(0).Value
            hCookie.Domain = Res.Cookies(0).Domain
            hCookie.Path = Res.Cookies(0).Path
            rs.Cookies.Add(hCookie)


            rs.Redirect(redirect)
            rs.Close()










Similar Threads
Thread Thread Starter Forum Replies Last Post
ProgressBar on httpwebrequest panuvin C# 1 February 5th, 2007 03:24 PM
HttpWebRequest over SSL Suhrit ASP.NET 2.0 Professional 0 December 7th, 2006 03:41 AM
HttpWebREsponse problem in ASP.net parthmankad ASP.NET 2.0 Basics 0 August 11th, 2006 01:34 AM
About HttpWebRequest in vb.net zhangxueq VS.NET 2002/2003 1 February 9th, 2004 12:30 PM
HTTPWebResponse returns partial data jeffki .NET Web Services 1 July 10th, 2003 04:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.