Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When I set a session value then redirect to another page I am able to retrieve the session value. As follows:

test-set.aspx
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Session("test") = "hello world!"
    Response.Redirect("test-get.aspx")
End Sub


test-get.aspx
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Response.Write("test = " & CType(Session("test"), String))
End Sub


The above results:
test = hello world!


However, when trying this via a HTTPWebRequest a new session is created for the redirected page.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim TestRequest As Net.HttpWebRequest = CType(System.Net.WebRequest.Create(Me.Request.Url.GetLeftPart(UriPartial.Authority) & "/test-set.aspx"), Net.HttpWebRequest)
    TestRequest.Method = "GET"
    Using TestResponse As Net.HttpWebResponse = CType(TestRequest.GetResponse, Net.HttpWebResponse)
        Using Reader As IO.StreamReader = New IO.StreamReader(TestResponse.GetResponseStream)
            Response.Write(Reader.ReadToEnd)
        End Using
    End Using
End Sub


Using HTTPWebRequest results as:
test = 


So the HTTPWebRequest retrieve the output from the redirected page but the redirected page is opened in a new session.

How can I open the redirected page in the same session it was redirected from?

What I have tried:

HttpWebRequest.AllowAutoRedirect

HttpWebRequest.MaximumAutomaticRedirections

HttpWebRequest.KeepAlive

HttpWebRequest.UseDefaultCredentials

Response.Redirect("test-get.aspx, True")

Response.Redirect("test-get.aspx, False")
Posted
Updated 17-Mar-19 10:32am
v2
Comments
F-ES Sitecore 16-Mar-19 7:39am    
Pages accessed via the browser can access your session because they send the session id as a cookie. When you create a request yourself via code it will only add to that request the things you tell it to add. If you want the page you are requesting to access the session then you'll need to add the session id as a cookie to the request you're sending.

You'll have to google for the exact code but it's going to be something along the lines of what appears here

http://geekswithblogs.net/xlinesonegoal/archive/2008/10/10/send-cookie-in-http-header-webrequest-asp.net-c-web-services.aspx
Er Avinash Kunnure 16-Mar-19 7:48am    
You are right
Inology 16-Mar-19 21:17pm    
thank you very much, that worked a treat!

1 solution

As F-ES Sitecore mentioned in the comment, I had to add the session ID. This was simply done with one line of code before GetResponse:

TestRequest.CookieContainer = New Net.CookieContainer()


So the updated request looks like:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim TestRequest As Net.HttpWebRequest = CType(System.Net.WebRequest.Create(Me.Request.Url.GetLeftPart(UriPartial.Authority) & "/test-set.aspx"), Net.HttpWebRequest)
    TestRequest.Method = "GET"
    TestRequest.CookieContainer = New Net.CookieContainer()
    Using TestResponse As Net.HttpWebResponse = CType(TestRequest.GetResponse, Net.HttpWebResponse)
        Using Reader As IO.StreamReader = New IO.StreamReader(TestResponse.GetResponseStream)
            Response.Write(Reader.ReadToEnd)
        End Using
    End Using
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900