Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > HttpWebRequest times out with ASP.NET_SessionId cookie

Reply
Thread Tools

HttpWebRequest times out with ASP.NET_SessionId cookie

 
 
Keith Patrick
Guest
Posts: n/a
 
      02-18-2005
I'm trying to programmatically post data to another page within my ASP.Net
app. Not POSTing is not an option (I can't store this data in my session,
context, hidden fields, or anything else...I've exhausted all my other
options, so I have to get this scenario working). Basically, if I POST the
data normally, it works fine, except the target page has a new session ID,
so I lost the session data that I *do* transfer around. However, if I
manually copy the cookies from the HttpContext.Current.Request to my
HttpWebRequest, the post only happens *after* a WebException (timeout)
happens, in which case, ASP.Net goes to the target page with the session
restored. However, because an exception caused it, I can't actually get
data from the response. Here is my code, so if anyone sees what I may be
doing wrong, please let me know:

System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)
System.Net.HttpWebRequest.Create(uri.AbsoluteUri);

request.Credentials =
System.Net.CredentialCache.DefaultCredentials;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
System.IO.Stream requestStream = request.GetRequestStream();
System.IO.StreamWriter requestWriter = new
System.IO.StreamWriter(requestStream);
requestWriter.Write(uri.AbsoluteUri);
requestWriter.Close();

requestStream.Close();

request.CookieContainer = new System.Net.CookieContainer();
foreach (String cookieName in
System.Web.HttpContext.Current.Request.Cookies) {
System.Web.HttpCookie cookie =
System.Web.HttpContext.Current.Request.Cookies[cookieName];
System.Net.Cookie bizarroCookie = new System.Net.Cookie();
bizarroCookie.Name = cookie.Name;
bizarroCookie.Value = cookie.Value;
bizarroCookie.Domain = uri.Host;
request.CookieContainer.Add(bizarroCookie);
}

System.Web.HttpContext.Current.Response.Close(); // This was an
attempt to fix that had no effect
System.Net.WebResponse response = request.GetResponse(); // This
is where I get the timeout


 
Reply With Quote
 
 
 
 
Rick Strahl [MVP]
Guest
Posts: n/a
 
      02-19-2005
Hi Keith,

I'm not sure how this app works, but I suspect the problem is that you need
a specifically assigned cookie from the server? All you're doing is making
up a cookie from scratch but it's not likely that you can generate a Session
ID client side unless the server's session scheme is very simple.

If so I would try to go to another page and try to capture a cookie from the
server (to make sure it's valid and server assigned) then resend that cookie
on your actual POST operation. IOW, you should hit two pages - the first to
get a cookie and hte second to do your thing and POST with the cookie you
retrieved from the first request.

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/weblog/
http://www.west-wind.com/wwThreads/
----------------------------------
Making waves on the Web


"Keith Patrick" <(E-Mail Removed)> wrote in message
news:3joRd.14183$(E-Mail Removed)...
> I'm trying to programmatically post data to another page within my ASP.Net
> app. Not POSTing is not an option (I can't store this data in my session,
> context, hidden fields, or anything else...I've exhausted all my other
> options, so I have to get this scenario working). Basically, if I POST

the
> data normally, it works fine, except the target page has a new session ID,
> so I lost the session data that I *do* transfer around. However, if I
> manually copy the cookies from the HttpContext.Current.Request to my
> HttpWebRequest, the post only happens *after* a WebException (timeout)
> happens, in which case, ASP.Net goes to the target page with the session
> restored. However, because an exception caused it, I can't actually get
> data from the response. Here is my code, so if anyone sees what I may be
> doing wrong, please let me know:
>
> System.Net.HttpWebRequest request =

(System.Net.HttpWebRequest)
> System.Net.HttpWebRequest.Create(uri.AbsoluteUri);
>
> request.Credentials =
> System.Net.CredentialCache.DefaultCredentials;
> request.Method = "POST";
> request.ContentType = "application/x-www-form-urlencoded";
> System.IO.Stream requestStream = request.GetRequestStream();
> System.IO.StreamWriter requestWriter = new
> System.IO.StreamWriter(requestStream);
> requestWriter.Write(uri.AbsoluteUri);
> requestWriter.Close();
>
> requestStream.Close();
>
> request.CookieContainer = new System.Net.CookieContainer();
> foreach (String cookieName in
> System.Web.HttpContext.Current.Request.Cookies) {
> System.Web.HttpCookie cookie =
> System.Web.HttpContext.Current.Request.Cookies[cookieName];
> System.Net.Cookie bizarroCookie = new System.Net.Cookie();
> bizarroCookie.Name = cookie.Name;
> bizarroCookie.Value = cookie.Value;
> bizarroCookie.Domain = uri.Host;
> request.CookieContainer.Add(bizarroCookie);
> }
>
> System.Web.HttpContext.Current.Response.Close(); // This was

an
> attempt to fix that had no effect
> System.Net.WebResponse response = request.GetResponse(); //

This
> is where I get the timeout
>
>



 
Reply With Quote
 
 
 
 
Keith Patrick
Guest
Posts: n/a
 
      02-19-2005
But the catch is, I can't get the session cookie (the System.Net.Cookie)
from a priming request because my app it authenticated already. The session
cookie has already been created, but it's a System.Web.HttpCookie instead.

I was figuring that my problem is that somehow the request isn't done yet,
even though I've been trying to close it with several different method
calls, because once the response *does* time out, the call goes through, and
my System.Web.HttpCookie-to-System.Net.Cookie transfer does result in the
session being restored. Maybe I can do a Thread.Abort or something similar
to kill the request (this is the request that I am trying to stop and re-do
as a POSTed request. I think the key is somewhere in
HttpContext.Current.Request or Response, but I just haven't hit upon the
right method to terminate it.


"Rick Strahl [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Keith,
>
> I'm not sure how this app works, but I suspect the problem is that you
> need
> a specifically assigned cookie from the server? All you're doing is making
> up a cookie from scratch but it's not likely that you can generate a
> Session
> ID client side unless the server's session scheme is very simple.
>
> If so I would try to go to another page and try to capture a cookie from
> the
> server (to make sure it's valid and server assigned) then resend that
> cookie
> on your actual POST operation. IOW, you should hit two pages - the first
> to
> get a cookie and hte second to do your thing and POST with the cookie you
> retrieved from the first request.
>
> +++ Rick ---
>
> --
>
> Rick Strahl
> West Wind Technologies
> http://www.west-wind.com/
> http://www.west-wind.com/weblog/
> http://www.west-wind.com/wwThreads/
> ----------------------------------
> Making waves on the Web
>
>
> "Keith Patrick" <(E-Mail Removed)> wrote in
> message
> news:3joRd.14183$(E-Mail Removed)...
>> I'm trying to programmatically post data to another page within my
>> ASP.Net
>> app. Not POSTing is not an option (I can't store this data in my
>> session,
>> context, hidden fields, or anything else...I've exhausted all my other
>> options, so I have to get this scenario working). Basically, if I POST

> the
>> data normally, it works fine, except the target page has a new session
>> ID,
>> so I lost the session data that I *do* transfer around. However, if I
>> manually copy the cookies from the HttpContext.Current.Request to my
>> HttpWebRequest, the post only happens *after* a WebException (timeout)
>> happens, in which case, ASP.Net goes to the target page with the session
>> restored. However, because an exception caused it, I can't actually get
>> data from the response. Here is my code, so if anyone sees what I may be
>> doing wrong, please let me know:
>>
>> System.Net.HttpWebRequest request =

> (System.Net.HttpWebRequest)
>> System.Net.HttpWebRequest.Create(uri.AbsoluteUri);
>>
>> request.Credentials =
>> System.Net.CredentialCache.DefaultCredentials;
>> request.Method = "POST";
>> request.ContentType =
>> "application/x-www-form-urlencoded";
>> System.IO.Stream requestStream = request.GetRequestStream();
>> System.IO.StreamWriter requestWriter = new
>> System.IO.StreamWriter(requestStream);
>> requestWriter.Write(uri.AbsoluteUri);
>> requestWriter.Close();
>>
>> requestStream.Close();
>>
>> request.CookieContainer = new System.Net.CookieContainer();
>> foreach (String cookieName in
>> System.Web.HttpContext.Current.Request.Cookies) {
>> System.Web.HttpCookie cookie =
>> System.Web.HttpContext.Current.Request.Cookies[cookieName];
>> System.Net.Cookie bizarroCookie = new
>> System.Net.Cookie();
>> bizarroCookie.Name = cookie.Name;
>> bizarroCookie.Value = cookie.Value;
>> bizarroCookie.Domain = uri.Host;
>> request.CookieContainer.Add(bizarroCookie);
>> }
>>
>> System.Web.HttpContext.Current.Response.Close(); // This was

> an
>> attempt to fix that had no effect
>> System.Net.WebResponse response = request.GetResponse(); //

> This
>> is where I get the timeout
>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

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
HttpWebRequest with java session id cookie; HELP PLEASE Amil ASP .Net 4 05-05-2011 12:06 PM
Server Times Out with ASPSESSIONID Cookie mike.biang@gmail.com ASP General 3 09-18-2006 09:26 PM
HttpWebRequest times out with ASP.NET_SessionId cookie (response TIMEOUT) davvel ASP .Net 1 06-13-2005 07:25 AM
IsAuthenticated times out with non-persistent cookie - Why/How? Kepler ASP .Net 0 10-27-2004 04:27 PM
Does session cookie persist when after session times out? =?Utf-8?B?RWFybCBUZWlncm9i?= ASP .Net 2 07-28-2004 06:42 PM



Advertisments