473,320 Members | 2,048 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,320 software developers and data experts.

sharing user info w/ a WebRequest ?

hello,

i am having trouble doing something. when a user triggers a certain
event in my app, i need to initiate another web request to one of my
other webpages, programmatically. currently, i do this via a WebRequest
-- i attach the default credentials and get the response stream. that
part works. like so:

WebRequest request = HttpWebRequest.Create(reportAspxURL);
request.Credentials = CredentialCache.DefaultCredentials;

WebResponse response = request.GetResponse();

//Read response to a string
Stream stream = response.GetResponseStream();
StreamReader sr = new StreamReader(stream);

string html = sr.ReadToEnd();

//insert html into my PDF-maker
ConvertToPDF(html);

what *doesnt* work, is trying to share user info between the user's
session in my app, and the web request (in this case, to the report
ASPX page). i need to pass some user info into the report-generating
ASPX. i understand that the Session objects cannot be shared, because
WebRequest (presumably) opens a new session.

but what about cookies? elsewhere in my app i use a cookie to store
session info, so when a user goes to a different virtual-directory in
the app, i can pick up their info seamlessly. but when i tried this
technique w/ the above (hitting the report-generating APSX), the .aspx
page cannot seem to find the cookie.
or is there perhaps a better way to do this? since its an
apppage-to-apppage call w/i the same project, can i programmatically
load up the 2nd page, rather than do a WebRequest?
thanks!
matt

Jul 18 '06 #1
6 1677
ma**@mailinator.com wrote:
but what about cookies? elsewhere in my app i use a cookie to store
session info, so when a user goes to a different virtual-directory in
the app, i can pick up their info seamlessly. but when i tried this
technique w/ the above (hitting the report-generating APSX), the .aspx
page cannot seem to find the cookie.
btw, heres the abstract helper-class code i use to set/get cookie info:

(first page, store some user value)

//create cookie
HttpCookie cookie = new HttpCookie(cookieName);

context.Response.Cookies.Add(cookie);

//optional cookie persist
if (rememberMe == true)
context.Response.Cookies[cookieName].Expires =
DateTime.Now.AddMonths(1);

//(called from elsewhere) add to cookie
HttpCookie cookie = context.Request.Cookies[cookieName];
cookie.Values[itemName] = itemValue;
context.Response.Cookies.Add(cookie);
(second page, retrieve some user value)

//get from cookie
HttpCookie cookie = context.Request.Cookies[cookieName];

if (cookie != null)
value = cookie.Values[itemName];
....like i mentioned, that works for my cross-virtual-directory pages.
but it doesnt seem to work when trying to retrieve cookie data from the
WebRequest .aspx page (same project/virtual-directory).
thanks!
matt

Jul 18 '06 #2
Matt,
at first reading it seems to me that you aren't "OOP-ifying" your code. If
some other page has a method that you need to call from your current page,
that method should be in a separate class library that your first page can
reference and use, without having to resort to a WebRequest. If you need
session, provide it with a reference to System.Web and / or pass in the
HttpContext.Current property to the method.
Peter

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


"ma**@mailinator.com" wrote:
ma**@mailinator.com wrote:
but what about cookies? elsewhere in my app i use a cookie to store
session info, so when a user goes to a different virtual-directory in
the app, i can pick up their info seamlessly. but when i tried this
technique w/ the above (hitting the report-generating APSX), the .aspx
page cannot seem to find the cookie.

btw, heres the abstract helper-class code i use to set/get cookie info:

(first page, store some user value)

//create cookie
HttpCookie cookie = new HttpCookie(cookieName);

context.Response.Cookies.Add(cookie);

//optional cookie persist
if (rememberMe == true)
context.Response.Cookies[cookieName].Expires =
DateTime.Now.AddMonths(1);

//(called from elsewhere) add to cookie
HttpCookie cookie = context.Request.Cookies[cookieName];
cookie.Values[itemName] = itemValue;
context.Response.Cookies.Add(cookie);
(second page, retrieve some user value)

//get from cookie
HttpCookie cookie = context.Request.Cookies[cookieName];

if (cookie != null)
value = cookie.Values[itemName];
....like i mentioned, that works for my cross-virtual-directory pages.
but it doesnt seem to work when trying to retrieve cookie data from the
WebRequest .aspx page (same project/virtual-directory).
thanks!
matt

Jul 18 '06 #3
peter,

often do i placed shared methods into common libraries. however that
isnt quite the situation here.
i have many .ASPX pages that produce reports, via DataRepeaters -- each
repeater uses a unique and sometimes complex HTML table to display the
results. in each report page, the Page_Load retrieves data & binds it
to that page's repeater (it does so w/ some user preferences as far as
the query goes).

now i need a PDF version for some of these reports. since i can pass in
HTML into a PDF-component, it seemed the thing to do was extract the
rendered HTML of a given report, and plop it into the PDF.

that seems sound, but the only problem is passing in the user's
preferences (stored in session) to the report -- no problem when they
were viewing the report as a webpage. problem when im trying to do a
WebRequest on the report.
i could certainly re-use the same data-generating method, as it is
indeed in a library. however, the DataRepeaters define HTML table at
design time, and it would seem trickier to learn how to produce said
table+repeaters at runtime, than it would be to figure out how to pass
state (user prefs) to these pages, and suck up their final HTML.
matt

Jul 18 '06 #4
Try using Cache instead of Session, but with a unique key such as the
sessionid, and pass that on the querystring.
Peter

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


"ma**@mailinator.com" wrote:
peter,

often do i placed shared methods into common libraries. however that
isnt quite the situation here.
i have many .ASPX pages that produce reports, via DataRepeaters -- each
repeater uses a unique and sometimes complex HTML table to display the
results. in each report page, the Page_Load retrieves data & binds it
to that page's repeater (it does so w/ some user preferences as far as
the query goes).

now i need a PDF version for some of these reports. since i can pass in
HTML into a PDF-component, it seemed the thing to do was extract the
rendered HTML of a given report, and plop it into the PDF.

that seems sound, but the only problem is passing in the user's
preferences (stored in session) to the report -- no problem when they
were viewing the report as a webpage. problem when im trying to do a
WebRequest on the report.
i could certainly re-use the same data-generating method, as it is
indeed in a library. however, the DataRepeaters define HTML table at
design time, and it would seem trickier to learn how to produce said
table+repeaters at runtime, than it would be to figure out how to pass
state (user prefs) to these pages, and suck up their final HTML.
matt

Jul 18 '06 #5
this won't work. access to session is serialized, so only one request (page)
can access the same session at a time. if you page calls another, that page
can not acces session until the calling page exits.

you could store your user info in a database (or application) and pass an id
to the url.

-- bruce (sqlwork.com)

<ma**@mailinator.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
hello,

i am having trouble doing something. when a user triggers a certain
event in my app, i need to initiate another web request to one of my
other webpages, programmatically. currently, i do this via a WebRequest
-- i attach the default credentials and get the response stream. that
part works. like so:

WebRequest request = HttpWebRequest.Create(reportAspxURL);
request.Credentials = CredentialCache.DefaultCredentials;

WebResponse response = request.GetResponse();

//Read response to a string
Stream stream = response.GetResponseStream();
StreamReader sr = new StreamReader(stream);

string html = sr.ReadToEnd();

//insert html into my PDF-maker
ConvertToPDF(html);

what *doesnt* work, is trying to share user info between the user's
session in my app, and the web request (in this case, to the report
ASPX page). i need to pass some user info into the report-generating
ASPX. i understand that the Session objects cannot be shared, because
WebRequest (presumably) opens a new session.

but what about cookies? elsewhere in my app i use a cookie to store
session info, so when a user goes to a different virtual-directory in
the app, i can pick up their info seamlessly. but when i tried this
technique w/ the above (hitting the report-generating APSX), the .aspx
page cannot seem to find the cookie.
or is there perhaps a better way to do this? since its an
apppage-to-apppage call w/i the same project, can i programmatically
load up the 2nd page, rather than do a WebRequest?
thanks!
matt

Jul 18 '06 #6

ma**@mailinator.com wrote:
or is there perhaps a better way to do this? since its an
apppage-to-apppage call w/i the same project, can i programmatically
load up the 2nd page, rather than do a WebRequest?
there was indeed a better way to do this. instead of a WebRequest
(useful for out-of-site grabs), use an Execute -- like so:

StringWriter writer = new StringWriter();
Server.Execute(""somePage.aspx", writer);

string bodyHTML = writer.ToString();

....and that grabs it. more, it also maintains state between the two!
excellent!
thanks,
matt

Jul 19 '06 #7

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

Similar topics

1
by: Meghna | last post by:
Hi, I was wondering if some one would be able to help me with this. I am trying to share a session across two servers and I seem to be experiencing some problems. I was wondering if anybody has...
1
by: Dennis Gavrilov | last post by:
Hi, All! I have two questions: strategic and technical. Technical one first: I need to share an array of objects (implemented as hashes, having references to other objects and hashes, sharing...
17
by: James Johnson | last post by:
Dear C#dex, I define a variable: HttpWebRequest webRequest and run the following request webRequest = WebRequest.Create(TARGET_URL) as HttpWebRequest; The webRequest object returns values...
0
by: John Dalberg | last post by:
Is there a Visual Studio plugin to enhance coding productivity? Say for example I want to program a function to go to a webpage and retrieve it. For a beginner, the person doesn't know what...
4
by: Piotr Strycharz | last post by:
Hi all I do have a problem. How can I transfer user to another server using POST. The problem is that Server.Transfer (preserves form data) works just in current server. Response.Redirect -...
1
by: Jon Spivey | last post by:
Hi, I'm using webrequest to scrape some urls, works fine, however one url seems to be throwing an error when it doesn't recognise the user agent. So I need to set the user agent to something...
8
by: antonyliu2002 | last post by:
We are extending a web application written in classic ASP long time ago. We will add more components to this web application in ASP.NET 2.0. To use the web application, our web users will have...
8
by: mc | last post by:
I would like to be able to send from an ASP.NET page an email which when recieved takes the form of a "Sharing Invitation for a RSS Feed"...
11
by: limperger | last post by:
Hello everybody! I have just found out that the sharing properties of a single database (mdb file on a network), accessed by 3 users, change depending on the user accessing the database. That is,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.