473,395 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to correctly carry over SessionID via HttpWebRequest?

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

Sep 5 '06 #1
10 29922
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]
- mv*@spam.guard.caspershouse.com

<rl********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>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

Sep 5 '06 #2
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]
- mv*@spam.guard.caspershouse.com

<rl********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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
Sep 5 '06 #3
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]
- mv*@spam.guard.caspershouse.com

<rl********@gmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
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]
- mv*@spam.guard.caspershouse.com

<rl********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googleg roups.com...
>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

Sep 5 '06 #4
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


"rl********@gmail.com" 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

Sep 5 '06 #5
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

Sep 5 '06 #6
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


"rl********@gmail.com" 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

Sep 5 '06 #7
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

Sep 5 '06 #8
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


"rl********@gmail.com" 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

Sep 6 '06 #9
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


"rl********@gmail.com" 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
Sep 6 '06 #10
You cannot influence directly the session variables, but you can send the session id (to keep it between calls).
In C# this is done by updating HttpWebRequest.Headers variable before making the requests. On my targetted site, the first response (without specifying the session id in the request) contained "SetCookie: PHPSESSID=12345...." in HttpWebResponse.Headers so starting with my 2nd request i added:

HttpWebRequest.Headers.Add("Cookie", "PHPSESSID=12345....")

just before making the requests, and it worked.
A http sniffer program is very helpful when you want to see what your code is creating, and comparing the headers to what an IE browser does.
Sep 21 '09 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Cliff Harris | last post by:
I am trying to automate a series of form posts on a website. This site requires that my session be kept alive through successive posts (it basically tracks me by a sessionid, and if I get a new...
0
by: Peter Qian | last post by:
Hi, I'm working on a windows form based program that can log into a web service (Apache based, https is used for auth). I was able to post the login data and obtain a sessionID. However I'm not...
2
by: Hardin | last post by:
I have an app that uses the sessionID to track user navigation and usage through the application. It works fine except in one case: There is a point in the application where I want to "close"...
2
by: XML newbie: Urgent pls help! | last post by:
If I get SessionID in 1 function how do I carry that SessionID(value of this SessionID) to another function or another form within the same project
0
by: John | last post by:
Hello, We have developed a web application which uses ASP.Net to persist a user's session state in SQLServer mode. The application makes use of this feature in a 2 server web farm, which is...
0
by: SRN | last post by:
I have some code to connect to a website and pull some content out of the HTML. I've verified that the 2 URLs I'm using are perfectly fine in Internet Explorer and both of them return results...
7
by: Marc Bartsch | last post by:
Hi, I have a background worker in my C# app that makes a synchronous HttpWebRequest.GetResponse() call. The idea is to POST a file to a server on the internet. When I call HttpWebRequest.Abort()...
1
by: ALA | last post by:
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?...
0
by: Crysnon | last post by:
My current goal is to be able to get the HTML source of an ASP.Net page that is hosted locally. Even though I make sure the session id is stored in the cookie container of the HttpWebRequest the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.