Get session cookies from webrequest and pass then to new request

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ThatsIT.net.au

    Get session cookies from webrequest and pass then to new request

    I am making a console app that requests pages from our site one after
    another. Each request starts a new session, What I want to do is make all
    requests in the same session.

    How can I do this.

    I was hoping to be able to get the session back from first request and then
    set them on each further request.

    Any ideas?

  • Hans Kesting

    #2
    Re: Get session cookies from webrequest and pass then to new request

    ThatsIT.net.au used his keyboard to write :
    I am making a console app that requests pages from our site one after
    another. Each request starts a new session, What I want to do is make all
    requests in the same session.
    >
    How can I do this.
    >
    I was hoping to be able to get the session back from first request and then
    set them on each further request.
    >
    Any ideas?
    There is a CookieContainer specifically for this scenario:
    create one and add it to every request you issue.

    Hans Kesting


    Comment

    • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

      #3
      Re: Get session cookies from webrequest and pass then to new request

      ThatsIT.net.au wrote:
      I am making a console app that requests pages from our site one after
      another. Each request starts a new session, What I want to do is make
      all requests in the same session.
      >
      How can I do this.
      >
      I was hoping to be able to get the session back from first request and
      then set them on each further request.
      Example of using CookieContainer :

      using System;
      using System.IO;
      using System.Net;

      namespace E
      {
      public class MainClass
      {
      public static string GetContent(stri ng url, CookieContainer
      session)
      {
      HttpWebRequest wr = (HttpWebRequest )WebRequest.Cre ate(url);
      wr.CookieContai ner = session;
      string html = (new
      StreamReader(wr .GetResponse(). GetResponseStre am())).ReadToEn d();
      return html;
      }
      public static void Main(string[] args)
      {
      CookieContainer session = new CookieContainer ();
      string login =
      GetContent("htt p://localhost:8080/logintest/login.jsp?usern ame=arne&passwo rd=hemmeligt",
      session);
      Console.WriteLi ne(login);
      string other =
      GetContent("htt p://localhost:8080/logintest/other.jsp", session);
      Console.WriteLi ne(other);
      }
      }
      }

      Arne

      Comment

      • ThatsIT.net.au

        #4
        Re: Get session cookies from webrequest and pass then to new request

        Sorry I did not get back earlier,

        thanks very much ill try it



        "Arne Vajhøj" <arne@vajhoej.d kwrote in message
        news:486cc494$0 $90262$14726298 @news.sunsite.d k...
        ThatsIT.net.au wrote:
        >I am making a console app that requests pages from our site one after
        >another. Each request starts a new session, What I want to do is make all
        >requests in the same session.
        >>
        >How can I do this.
        >>
        >I was hoping to be able to get the session back from first request and
        >then set them on each further request.
        >
        Example of using CookieContainer :
        >
        using System;
        using System.IO;
        using System.Net;
        >
        namespace E
        {
        public class MainClass
        {
        public static string GetContent(stri ng url, CookieContainer
        session)
        {
        HttpWebRequest wr = (HttpWebRequest )WebRequest.Cre ate(url);
        wr.CookieContai ner = session;
        string html = (new
        StreamReader(wr .GetResponse(). GetResponseStre am())).ReadToEn d();
        return html;
        }
        public static void Main(string[] args)
        {
        CookieContainer session = new CookieContainer ();
        string login =
        GetContent("htt p://localhost:8080/logintest/login.jsp?usern ame=arne&passwo rd=hemmeligt",
        session);
        Console.WriteLi ne(login);
        string other =
        GetContent("htt p://localhost:8080/logintest/other.jsp", session);
        Console.WriteLi ne(other);
        }
        }
        }
        >
        Arne

        Comment

        Working...