HttpWebRequest times out with ASP.NET_SessionId cookie


K

Keith Patrick

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
 
Ad

Advertisements

R

Rick Strahl [MVP]

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 said:
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
 
Ad

Advertisements

K

Keith Patrick

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


message
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
 

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