468,249 Members | 1,504 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,249 developers. It's quick & easy.

HttpWebRequest times out with ASP.NET_SessionId cookie

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
Nov 19 '05 #1
2 4508
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" <ri*******************@nospam.hotmail.com> wrote in message
news:3j******************@fe2.texas.rr.com...
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

Nov 19 '05 #2
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]" <ri********@hotmail.com> wrote in message
news:el**************@TK2MSFTNGP12.phx.gbl...
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" <ri*******************@nospam.hotmail.com> wrote in
message
news:3j******************@fe2.texas.rr.com...
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


Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Satinderpal Singh | last post: by
1 post views Thread by Satinderpal Singh | last post: by
11 posts views Thread by Keith Patrick | last post: by
1 post views Thread by ALA | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.