472,145 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,145 software developers and data experts.

HttpWebRequest and Session Information

ALA
Hi,

does anybody know if it is possible to pass the SessionID with a web
request by using a cookie so that the invoked page in the same domain
can access the session objects of the current user?

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = new CookieContainer();
HttpCookie sessionCookie =
HttpContext.Current.Request.Cookies["ASP.NET_SessionId"];
request.CookieContainer.Add(new Cookie(sessCookie.Name,
sessCookie.Value, "/", "localhost"));
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

This example doesn't seem to work for me but I do not know why... at
least I get a timeout when I execute the code above although the
mechanism works for every other cookie type, too. If I start a
HttpWebRequest without any session information, the target page has a
new and different sessionID than the soure page.

Thanks for the help

Andre

Oct 7 '07 #1
1 5457
In theory you create CookieContainer once and use it with every request.
HttpWebRequest will automaticly use it and update if needed.
You do not need to populate/repopulate it every time.

Example:
CookieContainer myContainer = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("login.aspx");
request.CookieContainer = myContainer;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
.......

request = (HttpWebRequest)WebRequest.Create("secondpage.aspx ");
request.CookieContainer = myContainer;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
PS: I belive the cookie's name fro ASP.NET session is changing all the time
(when .NET application restarts)
and it's different than ASP.NET_SessionId
So HttpContext.Current.Request.Cookies["ASP.NET_SessionId"];
should always be null.

George.


"ALA" <an**********@googlemail.comwrote in message
news:11*********************@o80g2000hse.googlegro ups.com...
Hi,

does anybody know if it is possible to pass the SessionID with a web
request by using a cookie so that the invoked page in the same domain
can access the session objects of the current user?

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = new CookieContainer();
HttpCookie sessionCookie =
HttpContext.Current.Request.Cookies["ASP.NET_SessionId"];
request.CookieContainer.Add(new Cookie(sessCookie.Name,
sessCookie.Value, "/", "localhost"));
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

This example doesn't seem to work for me but I do not know why... at
least I get a timeout when I execute the code above although the
mechanism works for every other cookie type, too. If I start a
HttpWebRequest without any session information, the target page has a
new and different sessionID than the soure page.

Thanks for the help

Andre

Oct 8 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Keith Patrick | last post: by
11 posts views Thread by Keith Patrick | last post: by
1 post views Thread by Manso | last post: by
3 posts views Thread by yoni | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.