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