Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7514899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:38:01+00:00 2026-05-30T00:38:01+00:00

I’m creating an application to do a few different things on a website in

  • 0

I’m creating an application to do a few different things on a website in the logged in section.

To do this, I must maintain a single continuous Cookie session for all requests I make.

So far I have successfully connected to the website via HttpWebRequest and the response has confirmed this, however I have been unable to reuse the cookie.

I have read all over SO and found topics that indicate how to use a cookie within the same function or class but I require the ability to use the cookie in multiple different functions.

My first thought was to try returning the cookie container from the initial login function and then pass it as a parameter to each subsequent function but I just couldn’t get it going.

Is anyone able to suggest either a better method or a way I can accomplish this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:38:02+00:00Added an answer on May 30, 2026 at 12:38 am

    I eventually came across the solution myself after much reading online. It appears the reason this didn’t work the first time I tried it was due to other settings that I had configured wrong.

    So for anyone else that may encounter the same issue, hope this helps:

    public const string userAgent = "Mozilla/5.0 (Windows NT 6.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1";
    public CookieContainer cookieJar;
    
    private void Login_Click(object sender, EventArgs e)
    {
        cookieJar = Login();
    }
    
    private CookieContainer Login()
    {
        string username = txtUsername.Text;
        string password = txtPassword.Text;
    
        // Create a request using the provied URL. 
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginPageURL);
    
        // Set the Method property of the request to POST.
        request.Method = "POST";
    
        // Set the Cookiecontainer
        CookieContainer cookieJar = new CookieContainer();
        request.CookieContainer = cookieJar;
    
        // Create POST data and convert it to a byte array.
        string postData = "username=" + username + "&password=" + password;
        byte[] byteArray = Encoding.UTF8.GetBytes (postData);
    
        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded";
    
        // Set the User Agent
        request.UserAgent = userAgent;
    
        // Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length;
    
        // Get the request stream.
        Stream dataStream = request.GetRequestStream ();
    
        // Write the data to the request stream.
        dataStream.Write (byteArray, 0, byteArray.Length);
    
        // Close the Stream object.
        dataStream.Close ();
    
        // Get the response.
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
        // Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream ();
    
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader (dataStream);
    
        // Read the content.
        string responseFromServer = reader.ReadToEnd ();
    
        string cookie = cookieJar.GetCookieHeader(request.RequestUri);
    
        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();
    
        return cookieJar;
    }
    
    private void viewPage(CookieContainer cookieJar, string pageURL)
    { 
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(pageURL);
    
        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded";
    
        // Set the Method property of the request to POST.
        request.Method = "POST";
    
        // Set the User Agent
        request.UserAgent = userAgent;
    
        // Put session back into CookieContainer
        request.CookieContainer = cookieJar;
    
        // Get the request stream.
        Stream dataStream = request.GetRequestStream();
    
        // Close the Stream object.
        dataStream.Close();
    
        // Get the response.
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
        // Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream();
    
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
    
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.