sharing user info w/ a WebRequest ?

  • Thread starter Thread starter matt
  • Start date Start date
M

matt

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




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
 
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
 
Try using Cache instead of Session, but with a unique key such as the
sessionid, and pass that on the querystring.
Peter
 
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)
 
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
 
Back
Top