How to correctly carry over SessionID via HttpWebRequest?

R

rlueneberg

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
R

rlueneberg

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


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 address 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
 
N

Nicholas Paldino [.NET/C# MVP]

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 address 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


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 address 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
 
G

Guest

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
 
R

rlueneberg

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
 
G

Guest

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
 
R

rlueneberg

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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top