HttpWebRequest times out with ASP.NET_SessionId cookie

Discussion in 'ASP .Net' started by Keith Patrick, Feb 18, 2005.

  1. 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
    Keith Patrick, Feb 18, 2005
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:3joRd.14183$...
    > 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
    >
    >
    Rick Strahl [MVP], Feb 19, 2005
    #2
    1. Advertising

  3. 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]" <> wrote in message
    news:...
    > 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" <> wrote in
    > message
    > news:3joRd.14183$...
    >> 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
    >>
    >>

    >
    >
    Keith Patrick, Feb 19, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Natty Gur
    Replies:
    0
    Views:
    1,228
    Natty Gur
    Jun 6, 2004
  2. Daniel Michaeloff

    Getting rid of session cookie ("ASP.NET_SessionId")

    Daniel Michaeloff, Dec 8, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    9,556
    Hans Kesting
    Dec 9, 2004
  3. davvel
    Replies:
    1
    Views:
    3,258
    David
    Jun 13, 2005
  4. Replies:
    0
    Views:
    1,363
  5. SevDer
    Replies:
    5
    Views:
    1,092
    Steven Cheng[MSFT]
    Aug 21, 2007
Loading...

Share This Page