By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,246 Members | 1,451 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,246 IT Pros & Developers. It's quick & easy.

HttpWebRequest and Session Information

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.