473,387 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 4787
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Satinderpal Singh | last post by:
Hi everyone, We are using HttpWebRequest to create a request to a URI, which requires us to login first. In order to process all the transactions, first we have to login and get the cookie value...
6
by: omyek | last post by:
I'm trying to mimic the browsing of a webpage using an HttpWebRequest. I've had a lot of luck with it so far, including logging into pages, posting form data, and even collecting and using cookies....
1
by: Satinderpal Singh | last post by:
Hi everyone, We are using HttpWebRequest to create a request to a URI, which requires us to login first. In order to process all the transactions, first we have to login and get the cookie value...
11
by: Keith Patrick | last post by:
Could someone explain to me the relationship between these two classes? I am ripping my hair out trying to divert an HttpRequest to a new location via an HttpWebRequest, but I cannot get my...
1
by: davvel | last post by:
It has been 6 days of re-writing the same code in different ways to try and avoid a getResponse Timeout which haunted me for much too long now. I am trying to do a very simple thing which is...
10
by: rlueneberg | last post by:
I am trying to foward the old sessionID using "Session.SessionID" to an HttpWebRequest CookieContainer so that I can capture the requested page session variables but it is not working as it is...
4
by: SevDer | last post by:
Hi, I've done some coding in my web application however right now for an unknown reason my asp.net 2.0 site is not setting asp.net_sessionid cookie and as a result, I am losing the session data...
1
by: ALA | last post by:
Hi, does anybody know if it is possible to pass the SessionID with a web request by using a cookie so that the invoked page in the same domain can access the session objects of the current user?...
3
by: dihola | last post by:
Hi, I have a website running in IIS7 and it seems to be creating a new session for every request I make. The values I store in Session are lost with every request. This is the forms bit in my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.