httpwebrequence cookie problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • octavio.filipe

    httpwebrequence cookie problem

    Hi,

    I'm trying to use the following code:


    HttpWebRequest webRequest = WebRequest.Crea te(LOGIN_URL) as HttpWebRequest;
    webRequest.Cook ieContainer = new CookieContainer ();

    HttpWebResponse webResponse = (HttpWebRespons e) webRequest.GetR esponse();


    This code let me get the cookie (cookie PHPSESSID) I need, from
    webResponse.
    I will need this cookie to get the page that i really need.
    Now I will copy the cookie to the HttpWebRequest that i will use to get the
    real needed page:


    HttpWebRequest httpWebRequestL ogin =
    HttpWebRequest. Create(LOGIN_UR L_LOGIN) as HttpWebRequest;
    httpWebRequestL ogin.CookieCont ainer = new CookieContainer ();
    //note: webResponse.Coo kies[0] have the PHPSESSID cookie (i know this)
    httpWebRequestL ogin.CookieCont ainer.Add(webRe sponse.Cookies[0]);
    httpWebRequestL ogin.Method="PO ST";
    httpWebRequestL ogin.ContentTyp e="applicatio n/x-www-form-urlencoded";

    StreamWriter requestWriter = new StreamWriter
    (httpWebRequest Login.GetReques tStream());
    requestWriter.W rite(data);
    requestWriter.C lose();

    //lets try to get the page
    HttpWebResponse httpWebResponse Login =
    (HttpWebRespons e) httpWebRequestL ogin.GetRespons e();


    The page didn't come out! Because the cookie PHPSESSID wasn't on the
    httpWebRequestL ogin, I think!
    So what in the name of LORD am I doing wrong (in the process of copying the
    cookie from the webResponse to the httWebRequestLo gin)?


    Please help me...
    Thanks on advance,
    o.f
  • Feroze [msft]

    #2
    Re: httpwebrequence cookie problem

    Why are you creating a new cookiecontainer for the next request? You should
    just reuse the first cookiecontainer that you created, and it should just
    work fine. WebRequest will send the cookie in that container to the new
    server, after making sure that the domains etc match.

    --
    feroze

    -----------------
    This posting is provided as-is. It offers no warranties and assigns no
    rights.

    See http://weblogs.asp.net/feroze_daud for System.Net related posts.
    ----------------

    "octavio.filipe " <0245078402@net cabo.pt> wrote in message
    news:Xns965BB47 263601024507840 2netcabopt@207. 46.248.16...[color=blue]
    > Hi,
    >
    > I'm trying to use the following code:
    >
    >
    > HttpWebRequest webRequest = WebRequest.Crea te(LOGIN_URL) as
    > HttpWebRequest;
    > webRequest.Cook ieContainer = new CookieContainer ();
    >
    > HttpWebResponse webResponse = (HttpWebRespons e) webRequest.GetR esponse();
    >
    >
    > This code let me get the cookie (cookie PHPSESSID) I need, from
    > webResponse.
    > I will need this cookie to get the page that i really need.
    > Now I will copy the cookie to the HttpWebRequest that i will use to get
    > the
    > real needed page:
    >
    >
    > HttpWebRequest httpWebRequestL ogin =
    > HttpWebRequest. Create(LOGIN_UR L_LOGIN) as HttpWebRequest;
    > httpWebRequestL ogin.CookieCont ainer = new CookieContainer ();
    > //note: webResponse.Coo kies[0] have the PHPSESSID cookie (i know this)
    > httpWebRequestL ogin.CookieCont ainer.Add(webRe sponse.Cookies[0]);
    > httpWebRequestL ogin.Method="PO ST";
    > httpWebRequestL ogin.ContentTyp e="applicatio n/x-www-form-urlencoded";
    >
    > StreamWriter requestWriter = new StreamWriter
    > (httpWebRequest Login.GetReques tStream());
    > requestWriter.W rite(data);
    > requestWriter.C lose();
    >
    > //lets try to get the page
    > HttpWebResponse httpWebResponse Login =
    > (HttpWebRespons e) httpWebRequestL ogin.GetRespons e();
    >
    >
    > The page didn't come out! Because the cookie PHPSESSID wasn't on the
    > httpWebRequestL ogin, I think!
    > So what in the name of LORD am I doing wrong (in the process of copying
    > the
    > cookie from the webResponse to the httWebRequestLo gin)?
    >
    >
    > Please help me...
    > Thanks on advance,
    > o.f[/color]


    Comment

    • Feroze [msft]

      #3
      Re: httpwebrequence cookie problem

      Why are you creating a new cookiecontainer for the next request? You should
      just reuse the first cookiecontainer that you created, and it should just
      work fine. WebRequest will send the cookie in that container to the new
      server, after making sure that the domains etc match.

      --
      feroze

      -----------------
      This posting is provided as-is. It offers no warranties and assigns no
      rights.

      See http://weblogs.asp.net/feroze_daud for System.Net related posts.
      ----------------

      "octavio.filipe " <0245078402@net cabo.pt> wrote in message
      news:Xns965BB47 263601024507840 2netcabopt@207. 46.248.16...[color=blue]
      > Hi,
      >
      > I'm trying to use the following code:
      >
      >
      > HttpWebRequest webRequest = WebRequest.Crea te(LOGIN_URL) as
      > HttpWebRequest;
      > webRequest.Cook ieContainer = new CookieContainer ();
      >
      > HttpWebResponse webResponse = (HttpWebRespons e) webRequest.GetR esponse();
      >
      >
      > This code let me get the cookie (cookie PHPSESSID) I need, from
      > webResponse.
      > I will need this cookie to get the page that i really need.
      > Now I will copy the cookie to the HttpWebRequest that i will use to get
      > the
      > real needed page:
      >
      >
      > HttpWebRequest httpWebRequestL ogin =
      > HttpWebRequest. Create(LOGIN_UR L_LOGIN) as HttpWebRequest;
      > httpWebRequestL ogin.CookieCont ainer = new CookieContainer ();
      > //note: webResponse.Coo kies[0] have the PHPSESSID cookie (i know this)
      > httpWebRequestL ogin.CookieCont ainer.Add(webRe sponse.Cookies[0]);
      > httpWebRequestL ogin.Method="PO ST";
      > httpWebRequestL ogin.ContentTyp e="applicatio n/x-www-form-urlencoded";
      >
      > StreamWriter requestWriter = new StreamWriter
      > (httpWebRequest Login.GetReques tStream());
      > requestWriter.W rite(data);
      > requestWriter.C lose();
      >
      > //lets try to get the page
      > HttpWebResponse httpWebResponse Login =
      > (HttpWebRespons e) httpWebRequestL ogin.GetRespons e();
      >
      >
      > The page didn't come out! Because the cookie PHPSESSID wasn't on the
      > httpWebRequestL ogin, I think!
      > So what in the name of LORD am I doing wrong (in the process of copying
      > the
      > cookie from the webResponse to the httWebRequestLo gin)?
      >
      >
      > Please help me...
      > Thanks on advance,
      > o.f[/color]


      Comment

      • o.f

        #4
        Re: httpwebrequence cookie problem

        What you are saying is that I should do something like this:

        CookieContainer cc = webRequest.Cook ieContainer;
        httpWebRequestL ogin.CookieCont ainer = cc;

        or should I use this:

        CookieContainer cc = new CookieContainer ();
        cc.add(webRespo nse.Cookies[0]);

        My doubt is: should i copy the CookieContainer from the WebRequest, or
        should I create a CookieContainer and add the Cookie that i whant from
        webResponse?
        This doubt exists because if I debug the response I can see the cookie
        i whant to copy, but if i debug the request i can't see the cookie i
        want anywhere!

        Thanks
        o.f

        On Thu, 19 May 2005 15:56:15 -0700, "Feroze [msft]"
        <ferozed@online .microsoft.com> wrote:
        [color=blue]
        >Why are you creating a new cookiecontainer for the next request? You should
        >just reuse the first cookiecontainer that you created, and it should just
        >work fine. WebRequest will send the cookie in that container to the new
        >server, after making sure that the domains etc match.[/color]

        Comment

        • Feroze [msft]

          #5
          Re: httpwebrequence cookie problem

          You shoud use the first option:

          CookieContainer cc = new CookieContainer ();
          httpWebRequest. CookieContainer = cc;

          and make sure you use the same cookiecontainer for all webrequest's

          --
          feroze

          -----------------
          This posting is provided as-is. It offers no warranties and assigns no
          rights.

          See http://weblogs.asp.net/feroze_daud for System.Net related posts.
          ----------------

          "o.f" <0245078402@net cabo.pt> wrote in message
          news:rtdr81du8s 6l9241udh67qlo9 e2fl3g013@4ax.c om...[color=blue]
          > What you are saying is that I should do something like this:
          >
          > CookieContainer cc = webRequest.Cook ieContainer;
          > httpWebRequestL ogin.CookieCont ainer = cc;
          >
          > or should I use this:
          >
          > CookieContainer cc = new CookieContainer ();
          > cc.add(webRespo nse.Cookies[0]);
          >
          > My doubt is: should i copy the CookieContainer from the WebRequest, or
          > should I create a CookieContainer and add the Cookie that i whant from
          > webResponse?
          > This doubt exists because if I debug the response I can see the cookie
          > i whant to copy, but if i debug the request i can't see the cookie i
          > want anywhere!
          >
          > Thanks
          > o.f
          >
          > On Thu, 19 May 2005 15:56:15 -0700, "Feroze [msft]"
          > <ferozed@online .microsoft.com> wrote:
          >[color=green]
          >>Why are you creating a new cookiecontainer for the next request? You
          >>should
          >>just reuse the first cookiecontainer that you created, and it should just
          >>work fine. WebRequest will send the cookie in that container to the new
          >>server, after making sure that the domains etc match.[/color]
          >[/color]


          Comment

          Working...