PC Review


Reply
Thread Tools Rating: Thread Rating: 3 votes, 1.00 average.

How to correctly carry over SessionID via HttpWebRequest?

 
 
rlueneberg@gmail.com
Guest
Posts: n/a
 
      5th Sep 2006
I am trying to foward the old sessionID using "Session.SessionID" to
an HttpWebRequest CookieContainer so that I can capture the requested
page session variables but it is not working as it is supposed to. The
HttpResponse object always returns a different sessionID from the old
one which I am trying to force. Why is objRequest not carrying over the
old SessionID?

private String ReadHtmlPage(string url)
{
String result = string.Empty;
CookieContainer myContainer = new CookieContainer();
HttpWebRequest objRequest =
(HttpWebRequest)WebRequest.Create(url);
objRequest.Method = "GET";
objRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE
6.0; Windows NT5.0; .NET CLR 1.0.2914)";
objRequest.CookieContainer = new CookieContainer();
Cookie c = new Cookie();
c.Name = "ASP.NET_SessionId";
c.Value = Session.SessionID;
c.Domain = "http://localhost/mysite/";
myContainer.Add(c);
Response.Write("OLD SessionID -> " + Session.SessionID +
"<br>");
HttpWebResponse objResponse =
(HttpWebResponse)objRequest.GetResponse();
//retain the cookies
foreach (Cookie cook in objResponse.Cookies)
{
Response.Write("New SessionID -> " + cook.Name +
cook.Value + cook.Domain + "<br>");
}

//Check out the html.
using (StreamReader sr =
new StreamReader(objResponse.GetResponseStream())
)
{
result = sr.ReadToEnd();

// Close and clean up the StreamReader
sr.Close();
}

return result;
}

Thanks
Rod

 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      5th Sep 2006
Rod,

Why not just store the CookieContainer instance which you attached to
the first HttpWebRequest, and then attach that to the second HttpWebRequest?
This way, the cookies are passed from request to request.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I am trying to foward the old sessionID using "Session.SessionID" to
> an HttpWebRequest CookieContainer so that I can capture the requested
> page session variables but it is not working as it is supposed to. The
> HttpResponse object always returns a different sessionID from the old
> one which I am trying to force. Why is objRequest not carrying over the
> old SessionID?
>
> private String ReadHtmlPage(string url)
> {
> String result = string.Empty;
> CookieContainer myContainer = new CookieContainer();
> HttpWebRequest objRequest =
> (HttpWebRequest)WebRequest.Create(url);
> objRequest.Method = "GET";
> objRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE
> 6.0; Windows NT5.0; .NET CLR 1.0.2914)";
> objRequest.CookieContainer = new CookieContainer();
> Cookie c = new Cookie();
> c.Name = "ASP.NET_SessionId";
> c.Value = Session.SessionID;
> c.Domain = "http://localhost/mysite/";
> myContainer.Add(c);
> Response.Write("OLD SessionID -> " + Session.SessionID +
> "<br>");
> HttpWebResponse objResponse =
> (HttpWebResponse)objRequest.GetResponse();
> //retain the cookies
> foreach (Cookie cook in objResponse.Cookies)
> {
> Response.Write("New SessionID -> " + cook.Name +
> cook.Value + cook.Domain + "<br>");
> }
>
> //Check out the html.
> using (StreamReader sr =
> new StreamReader(objResponse.GetResponseStream())
> )
> {
> result = sr.ReadToEnd();
>
> // Close and clean up the StreamReader
> sr.Close();
> }
>
> return result;
> }
>
> Thanks
> Rod
>



 
Reply With Quote
 
rlueneberg@gmail.com
Guest
Posts: n/a
 
      5th Sep 2006
Nicholas,

That is the thing. There are no two requests, just one. Why would I
need to have two web requests if there is just one page to be
requested? I am assuming that Session.SessionID will grab the value for
the current session, correct? I am confused... sorry..

Rod



Paldino [.NET/C# MVP] wrote:
> Rod,
>
> Why not just store the CookieContainer instance which you attached to
> the first HttpWebRequest, and then attach that to the second HttpWebRequest?
> This way, the cookies are passed from request to request.
>
> Hope this helps.
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - (E-Mail Removed)
>
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >I am trying to foward the old sessionID using "Session.SessionID" to
> > an HttpWebRequest CookieContainer so that I can capture the requested
> > page session variables but it is not working as it is supposed to. The
> > HttpResponse object always returns a different sessionID from the old
> > one which I am trying to force. Why is objRequest not carrying over the
> > old SessionID?
> >
> > private String ReadHtmlPage(string url)
> > {
> > String result = string.Empty;
> > CookieContainer myContainer = new CookieContainer();
> > HttpWebRequest objRequest =
> > (HttpWebRequest)WebRequest.Create(url);
> > objRequest.Method = "GET";
> > objRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE
> > 6.0; Windows NT5.0; .NET CLR 1.0.2914)";
> > objRequest.CookieContainer = new CookieContainer();
> > Cookie c = new Cookie();
> > c.Name = "ASP.NET_SessionId";
> > c.Value = Session.SessionID;
> > c.Domain = "http://localhost/mysite/";
> > myContainer.Add(c);
> > Response.Write("OLD SessionID -> " + Session.SessionID +
> > "<br>");
> > HttpWebResponse objResponse =
> > (HttpWebResponse)objRequest.GetResponse();
> > //retain the cookies
> > foreach (Cookie cook in objResponse.Cookies)
> > {
> > Response.Write("New SessionID -> " + cook.Name +
> > cook.Value + cook.Domain + "<br>");
> > }
> >
> > //Check out the html.
> > using (StreamReader sr =
> > new StreamReader(objResponse.GetResponseStream())
> > )
> > {
> > result = sr.ReadToEnd();
> >
> > // Close and clean up the StreamReader
> > sr.Close();
> > }
> >
> > return result;
> > }
> >
> > Thanks
> > Rod
> >


 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      5th Sep 2006
Rod,

You are right, there isn't two requests for one page, but if you are
only ever going to download one page from the site, then why do you need the
session id?


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Nicholas,
>
> That is the thing. There are no two requests, just one. Why would I
> need to have two web requests if there is just one page to be
> requested? I am assuming that Session.SessionID will grab the value for
> the current session, correct? I am confused... sorry..
>
> Rod
>
>
>
> Paldino [.NET/C# MVP] wrote:
>> Rod,
>>
>> Why not just store the CookieContainer instance which you attached to
>> the first HttpWebRequest, and then attach that to the second
>> HttpWebRequest?
>> This way, the cookies are passed from request to request.
>>
>> Hope this helps.
>>
>> --
>> - Nicholas Paldino [.NET/C# MVP]
>> - (E-Mail Removed)
>>
>> <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> >I am trying to foward the old sessionID using "Session.SessionID" to
>> > an HttpWebRequest CookieContainer so that I can capture the requested
>> > page session variables but it is not working as it is supposed to. The
>> > HttpResponse object always returns a different sessionID from the old
>> > one which I am trying to force. Why is objRequest not carrying over the
>> > old SessionID?
>> >
>> > private String ReadHtmlPage(string url)
>> > {
>> > String result = string.Empty;
>> > CookieContainer myContainer = new CookieContainer();
>> > HttpWebRequest objRequest =
>> > (HttpWebRequest)WebRequest.Create(url);
>> > objRequest.Method = "GET";
>> > objRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE
>> > 6.0; Windows NT5.0; .NET CLR 1.0.2914)";
>> > objRequest.CookieContainer = new CookieContainer();
>> > Cookie c = new Cookie();
>> > c.Name = "ASP.NET_SessionId";
>> > c.Value = Session.SessionID;
>> > c.Domain = "http://localhost/mysite/";
>> > myContainer.Add(c);
>> > Response.Write("OLD SessionID -> " + Session.SessionID +
>> > "<br>");
>> > HttpWebResponse objResponse =
>> > (HttpWebResponse)objRequest.GetResponse();
>> > //retain the cookies
>> > foreach (Cookie cook in objResponse.Cookies)
>> > {
>> > Response.Write("New SessionID -> " + cook.Name +
>> > cook.Value + cook.Domain + "<br>");
>> > }
>> >
>> > //Check out the html.
>> > using (StreamReader sr =
>> > new StreamReader(objResponse.GetResponseStream())
>> > )
>> > {
>> > result = sr.ReadToEnd();
>> >
>> > // Close and clean up the StreamReader
>> > sr.Close();
>> > }
>> >
>> > return result;
>> > }
>> >
>> > Thanks
>> > Rod
>> >

>



 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      5th Sep 2006
Rod,
I think Nick more or less alluded to this, but if you are requesting a page
via HttpWebRequest, all you are going to get is the HTML of the page. You
aren't going to magically have access to Session State, which exists solely
server-side, even if you get the session cookie.

What's the goal here?

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




"(E-Mail Removed)" wrote:

> I am trying to foward the old sessionID using "Session.SessionID" to
> an HttpWebRequest CookieContainer so that I can capture the requested
> page session variables but it is not working as it is supposed to. The
> HttpResponse object always returns a different sessionID from the old
> one which I am trying to force. Why is objRequest not carrying over the
> old SessionID?
>
> private String ReadHtmlPage(string url)
> {
> String result = string.Empty;
> CookieContainer myContainer = new CookieContainer();
> HttpWebRequest objRequest =
> (HttpWebRequest)WebRequest.Create(url);
> objRequest.Method = "GET";
> objRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE
> 6.0; Windows NT5.0; .NET CLR 1.0.2914)";
> objRequest.CookieContainer = new CookieContainer();
> Cookie c = new Cookie();
> c.Name = "ASP.NET_SessionId";
> c.Value = Session.SessionID;
> c.Domain = "http://localhost/mysite/";
> myContainer.Add(c);
> Response.Write("OLD SessionID -> " + Session.SessionID +
> "<br>");
> HttpWebResponse objResponse =
> (HttpWebResponse)objRequest.GetResponse();
> //retain the cookies
> foreach (Cookie cook in objResponse.Cookies)
> {
> Response.Write("New SessionID -> " + cook.Name +
> cook.Value + cook.Domain + "<br>");
> }
>
> //Check out the html.
> using (StreamReader sr =
> new StreamReader(objResponse.GetResponseStream())
> )
> {
> result = sr.ReadToEnd();
>
> // Close and clean up the StreamReader
> sr.Close();
> }
>
> return result;
> }
>
> Thanks
> Rod
>
>

 
Reply With Quote
 
rlueneberg@gmail.com
Guest
Posts: n/a
 
      5th Sep 2006
Answering the previous question: if you are only ever going to download
one page from the site, then why do you need the session id?

Answer: It is one single request that passes a session value to the
requested page. Ps: It can't be passed via URL. The requested page is
built based on the value of this session.

The HTTPrequest does not work because when it fetches the page the
session variable is always null. I think now you go my point. I thought
SessionID would be the reference in Server side to maintain Session
State between on request and the other....But If technique doesn't
work what would be the solution and how to maintain session state in
this scenario?

Thank you

Rod

 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      5th Sep 2006
Rod,
I am sorry but unless the "Requested page" is prepared to either accept a
form name=value pair or a querystring name=value pair and store this in
Session, I cannot think of any other way this could be done.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




"(E-Mail Removed)" wrote:

> Answering the previous question: if you are only ever going to download
> one page from the site, then why do you need the session id?
>
> Answer: It is one single request that passes a session value to the
> requested page. Ps: It can't be passed via URL. The requested page is
> built based on the value of this session.
>
> The HTTPrequest does not work because when it fetches the page the
> session variable is always null. I think now you go my point. I thought
> SessionID would be the reference in Server side to maintain Session
> State between on request and the other....But If technique doesn't
> work what would be the solution and how to maintain session state in
> this scenario?
>
> Thank you
>
> Rod
>
>

 
Reply With Quote
 
rlueneberg@gmail.com
Guest
Posts: n/a
 
      5th Sep 2006
Thanks.Just to close the case in a lot of cases you want to extract
existing pieces of HTML code that reside in different pages and do not
want to recreate or modify them. A good example is a shopping cart
where the state of the cart depends on several Session Variables like
CustomerID, Language, Locale, etc. And each page is built dynamically
based on these values. According to your post that means I can't use
HttpRequest unless I modify the whole shopping cart structure to
support post and Querystrings?

Rod

 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      6th Sep 2006
Just keep in mind that a Session variable exists only at the server, in your
codebehind code. You can choose to put this value into the page, in a label,
or even a hidden formfield, so that you can get at it via HttpWebRequest or
XMLHTTP "Webscraping". However, unless your GET url contains name/value pairs
on the querystring, or your POST method contains formfields and the page is
wired up to retrieve these and store them in session state, that's about it.

Usually, there are other ways to accomplish what you want to do.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




"(E-Mail Removed)" wrote:

> Thanks.Just to close the case in a lot of cases you want to extract
> existing pieces of HTML code that reside in different pages and do not
> want to recreate or modify them. A good example is a shopping cart
> where the state of the cart depends on several Session Variables like
> CustomerID, Language, Locale, etc. And each page is built dynamically
> based on these values. According to your post that means I can't use
> HttpRequest unless I modify the whole shopping cart structure to
> support post and Querystrings?
>
> Rod
>
>

 
Reply With Quote
 
rlueneberg@gmail.com
Guest
Posts: n/a
 
      6th Sep 2006
Thank Peter. That was very helpful.. I appreciate you time.

Rod


Peter wrote:
> Just keep in mind that a Session variable exists only at the server, in your
> codebehind code. You can choose to put this value into the page, in a label,
> or even a hidden formfield, so that you can get at it via HttpWebRequest or
> XMLHTTP "Webscraping". However, unless your GET url contains name/value pairs
> on the querystring, or your POST method contains formfields and the page is
> wired up to retrieve these and store them in session state, that's about it.
>
> Usually, there are other ways to accomplish what you want to do.
> Peter
>
> --
> Co-founder, Eggheadcafe.com developer portal:
> http://www.eggheadcafe.com
> UnBlog:
> http://petesbloggerama.blogspot.com
>
>
>
>
> "(E-Mail Removed)" wrote:
>
> > Thanks.Just to close the case in a lot of cases you want to extract
> > existing pieces of HTML code that reside in different pages and do not
> > want to recreate or modify them. A good example is a shopping cart
> > where the state of the cart depends on several Session Variables like
> > CustomerID, Language, Locale, etc. And each page is built dynamically
> > based on these values. According to your post that means I can't use
> > HttpRequest unless I modify the whole shopping cart structure to
> > support post and Querystrings?
> >
> > Rod
> >
> >


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why doesn't Word 2003 carry out a spell check correctly? =?Utf-8?B?QmlsbA==?= Microsoft Word Document Management 1 25th Sep 2004 10:09 PM
auto-fill, set carry to, set carry on etc vw Microsoft Access Forms 1 4th Sep 2004 07:29 AM
Passing active SessionID to a HttpWebRequest michi Microsoft Dot NET Framework 3 23rd May 2004 09:40 AM
generate own unique sessionid instead standard asp.net 120bit sessionid Ronald Microsoft ASP .NET 6 23rd Feb 2004 08:03 AM
HttpWebRequest not accessing a logged in page correctly omyek@yahoo.com Microsoft C# .NET 5 4th Dec 2003 07:46 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:33 PM.