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 6997999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:21:17+00:00 2026-05-27T20:21:17+00:00

I have created a C# application that connects to a my phpBB forum and

  • 0

I have created a C# application that connects to a my phpBB forum and logs in. I am now trying to use a webclient to go to a page and grab the page as a string. However I keep getting logged out. How do I use the cookie created by the code used to log onto the forum, in the webclient?

Code used to log onto forum and get page:

       public static CookieContainer login(string url, string username, string password, Form1 form)
       {
           if (url.Length == 0 || username.Length == 0 || password.Length == 0)
           {
               Console.WriteLine("Information missing");
               return null;
           }

           CookieContainer myContainer = new CookieContainer();

           HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
           request.CookieContainer = myContainer;

           // Set type to POST
           request.Method = "POST";
           request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
           request.ContentType = "application/x-www-form-urlencoded";

           // Build the new header, this isn't a multipart/form, so it's very simple
           StringBuilder data = new StringBuilder();
           data.Append("username=" + Uri.EscapeDataString(username));
           data.Append("&password=" + Uri.EscapeDataString(password));
           data.Append("&login=Login");

           // Create a byte array of the data we want to send
           byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());

           // Set the content length in the request headers
           request.ContentLength = byteData.Length;

           Stream postStream;
           try
           {
               postStream = request.GetRequestStream();
           }
           catch (Exception e)
           {
               Console.WriteLine("Login - " + e.Message.ToString() + " (GRS)");
               return null;
           }

           // Write data
           postStream.Write(byteData, 0, byteData.Length);

           HttpWebResponse response;
           try
           {
               response = (HttpWebResponse)request.GetResponse();
           }
           catch (Exception e)
           {
               Console.WriteLine("Login - " + e.Message.ToString() + " (GR)");
               return null;
           }

           bool isLoggedIn = false;

           // Store the cookies
           foreach (Cookie c in response.Cookies)
           {
               if (c.Name.Contains("_u"))
               {
                   if (Convert.ToInt32(c.Value) > 1)
                   {
                       isLoggedIn = true;

                   }
               }
               myContainer.Add(c);
           }

           if (isLoggedIn)
           {

               string _url = "http://www.dandrews.net/forum/custom.php";
               string strResult = "";

               HttpWebRequest _request = (HttpWebRequest)HttpWebRequest.Create(_url);
               _request.CookieContainer = myContainer;
               HttpWebResponse _response = (HttpWebResponse)_request.GetResponse();



               using (StreamReader sr = new StreamReader(_response.GetResponseStream()))
               {
                   strResult = sr.ReadToEnd();
                   // Close and clean up the StreamReader
                   sr.Close();
               }
               form.userbox.Text = strResult;

               return myContainer;

           }
           else
           {
               return null;
           }
       }
  • 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-27T20:21:18+00:00Added an answer on May 27, 2026 at 8:21 pm

    Your using the CookieContainer differently than designed.

    CookieContainer myContainer = new CookieContainer();
    
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.CookieContainer = new CookieContainer();
    

    Should be:

    CookieContainer myContainer = new CookieContainer();
    
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.CookieContainer = myContainer;
    

    Then you can completely remove your logic to populate the container yourself because the framework will do that for you. Then just be sure to use the myContainer instance in your second request and it should work.

    Update

    If you must use the WebClient class for your second request, you may want to check out this question to help you use the CookieContainer with WebClient requests.

    Update

    Based on your updated code:

         public static CookieContainer login(string url, string username, string password)
        {
            if (url.Length == 0 || username.Length == 0 || password.Length == 0)
            {
                Console.WriteLine("Information missing");
                return null;
            }
    
            CookieContainer myContainer = new CookieContainer();
    
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.CookieContainer = myContainer;
    
            // Set type to POST
            request.Method = "POST";
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
            request.ContentType = "application/x-www-form-urlencoded";
    
            // Build the new header, this isn't a multipart/form, so it's very simple
            StringBuilder data = new StringBuilder();
            data.Append("username=" + Uri.EscapeDataString(username));
            data.Append("&password=" + Uri.EscapeDataString(password));
            data.Append("&login=Login");
    
            // Create a byte array of the data we want to send
            byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
    
            // Set the content length in the request headers
            request.ContentLength = byteData.Length;
    
            Stream postStream;
            try
            {
                postStream = request.GetRequestStream();
            }
            catch (Exception e)
            {
                Console.WriteLine("Login - " + e.Message.ToString() + " (GRS)");
                return null;
            }
    
            // Write data
            postStream.Write(byteData, 0, byteData.Length);
    
            HttpWebResponse response;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (Exception e)
            {
                Console.WriteLine("Login - " + e.Message.ToString() + " (GR)");
                return null;
            }
            string _url = "http://www.dandrews.net/forum/custom.php";
    
            // Store the cookies
            if (myContainer.GetCookies(new Uri(url)).Cast<Cookie>().Any(c => c.Name.Contains("_u")))
            {
                string strResult = "";
    
                HttpWebRequest _request = (HttpWebRequest)HttpWebRequest.Create(_url);
                _request.CookieContainer = myContainer;
                HttpWebResponse _response = (HttpWebResponse)_request.GetResponse();
    
                using (StreamReader sr = new StreamReader(_response.GetResponseStream()))
                {
                    strResult = sr.ReadToEnd();
                    // Close and clean up the StreamReader
                    sr.Close();
                }
                Console.WriteLine(strResult);
    
                return myContainer;
            }
            else
            {
                return null;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a timeclock application in C# that connects to a web service
I have an application that connects fine over http. When trying https I got
I have created an application that writes some data to the root folder of
I have created an application that runs in the taskbar. When a user clicks
I have created an application that has a toolbar, menubar and content area. I
I have created an application that uses settings.settings to store some user specific settings
I have created an application that records a series of longitude and latitude values
I have created a console application that calls a method on a webservice. I
I have created one application in flex that is accessing the Java webservice using
I have created a new ActionFilter for an ASP.NET MVC application that I'm creating.

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.