Issue Accessing Cookies

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mcfly1204
    New Member
    • Jul 2007
    • 233

    Issue Accessing Cookies

    I have a request to a login page, and I am trying to save the response cookies in a CookieContainer . I am then attempting to make another request reusing the previous CookieContainer . I then am trying to save the content of the second response as an .html document. However, it seems as though my second request keeps taking me back to the original URI (login page).

    Code:
    public void selectFiles()
                {
                    CookieContainer myContainer = new CookieContainer();
    
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://subdomain.domain.com/login.aspx");
                    request.CookieContainer = myContainer;
                    request.Credentials = new NetworkCredential("un", "pw");
                    request.AllowAutoRedirect = false;
                    request.KeepAlive = true;
                    request.Method = "POST";
                    request.ContentType = "text/html";
                    request.ContentLength = 0;
    
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                    HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create("https://subdomain.domain.com/Default.aspx?GUID=123456");
                    request2.CookieContainer = myContainer;
    
                    HttpWebResponse response2 = (HttpWebResponse)request2.GetResponse();
    
                    try
                    {
                        Stream responseStream = response2.GetResponseStream();
                        Encoding enc = Encoding.GetEncoding(response2.CharacterSet);
                        StreamReader reader = new StreamReader(responseStream, enc);
                        Stream fileStream = File.OpenWrite(@"c:\test1234.html");
                        using (StreamWriter sw = new StreamWriter(fileStream, enc))
                        {
                            sw.Write(reader.ReadToEnd());
                            sw.Flush();
                            sw.Close();
                        }
                    }
                    finally
                    {
                        response.Close();
                    }
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Please enclose your posted code in [CODE] [/CODE] tags (See How to Ask a Question). Code tags preserve indention and uses a monospaced font.

    This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

    Please use [CODE] [/CODE] tags in future.

    MODERATOR

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      I've never actually tried to do what you're doing here so I looked into your problem.
      After checking out the HttpWebRequest. CookieContainer Property

      I discovered that:

      The CookieContainer property provides an instance of the CookieContainer class that contains the cookies associated with this request.

      CookieContainer is nullNothingnull ptra null reference (Nothing in Visual Basic) by default. You must assign a CookieContainer object to the property to have cookies returned in the Cookies property of the HttpWebResponse returned by the GetResponse method.


      I have a feeling that no cookies are going to be in your cookies container after executing the GetResponse() method...

      I think you're going to have to access the cookies using the Cookies property of the HttpWebResponse returned by the GetResponse()

      -Frinny

      Comment

      • mcfly1204
        New Member
        • Jul 2007
        • 233

        #4
        I suppose I need to be certain of what exactly I am trying to do here. I am trying to access a URI, and I am redirected here (https://springboard.4imprint.com) to authenticate. The login page appears to use form authentication as well as cookies. When logged in via a browser, you are redirected back to the original URI. How should I go about the authentication process? HttpWebRequest using a CookieContainer to store cookies. Do I need to use a stream writer to send login information in the request? If I understand correctly, I cannot use NetworkCredenti al for forms authentication. Any help would be appreciated.

        Comment

        • mcfly1204
          New Member
          • Jul 2007
          • 233

          #5
          Originally posted by Frinavale
          I've never actually tried to do what you're doing here so I looked into your problem.
          After checking out the HttpWebRequest. CookieContainer Property

          I discovered that:

          The CookieContainer property provides an instance of the CookieContainer class that contains the cookies associated with this request.

          CookieContainer is nullNothingnull ptra null reference (Nothing in Visual Basic) by default. You must assign a CookieContainer object to the property to have cookies returned in the Cookies property of the HttpWebResponse returned by the GetResponse method.


          I have a feeling that no cookies are going to be in your cookies container after executing the GetResponse() method...

          I think you're going to have to access the cookies using the Cookies property of the HttpWebResponse returned by the GetResponse()

          -Frinny
          I appreciate your response, but the following shows otherwise,
          Code:
          Console.WriteLine(myContainer.Count.ToString());
          Also, I can attempt to add cookies from the response to the container,
          Code:
          foreach (Cookie cook in response.Cookies)
                          {
                              myContainer.Add(new Cookie(cook.Name, cook.Value, cook.Path, cook.Domain));
                          }
          and then get a count, but it remains the same.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Check out this article and this article.
            It seems to me that this is not going to be easy to do.

            Have you considered using a WebService instead of screen scraping the ASPX page?

            -Frinny

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              Originally posted by mcfly1204
              I appreciate your response, but the following shows otherwise,
              Code:
              Console.WriteLine(myContainer.Count.ToString());
              Also, I can attempt to add cookies from the response to the container,
              Code:
              foreach (Cookie cook in response.Cookies)
                              {
                                  myContainer.Add(new Cookie(cook.Name, cook.Value, cook.Path, cook.Domain));
                              }
              and then get a count, but it remains the same.
              Sorry, like I said I haven't actually tried this before....it was just something that I thought might be the problem.

              Check out the articles that I posted and see if they help.

              Comment

              • mcfly1204
                New Member
                • Jul 2007
                • 233

                #8
                Thanks for the links, I had been working with the example in the second link, but have hit a wall. Upon performing the HttpWebRequest of method type "POST", response.GetRes ponseHeader("Lo cation") returns null. I should also note that no redirect is occurring here, I am receiving a response 200 as opposed to the desired 302. I would image the location header is null because the redirect is not occuring.

                Comment

                Working...