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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:41:08+00:00 2026-06-04T13:41:08+00:00

I’m quite new at c# and am trying to build a program that logs

  • 0

I’m quite new at c# and am trying to build a program that logs into a website and returns it’s source code. Problem is, I register an event listener for when the page is loaded, but when I debug it it quits after setting the same event, not actually doing what I want it to do after the page “loads”.

Here’s the source –

using System;
using System.Windows.Forms;

namespace WIN
{
    class Program
    {
        string url = -snip-;
        string username = -snip-;
        string password = -snip-;
        string task = -snip-;
        string action = -snip-;
        string timezone = -snip-;

        private void Login()
        {
            Console.WriteLine("Started.");
            Console.ReadLine();
            Console.WriteLine("Declaring WebBrowser instance browser...");
            WebBrowser browser = new WebBrowser();
            Console.WriteLine("Done.");
            Console.ReadLine();
            Console.WriteLine("Registering an event for when the page finishes loading...");
            browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(pageLoaded);
            Console.WriteLine("Done.");
            Console.ReadLine();
            Console.WriteLine("Using method Navigate of browser instance with url parameter...");
            browser.Navigate(url);
            Console.WriteLine("Done.");
            Console.ReadLine();

        }

        private void pageLoaded(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            Console.WriteLine("Declaring WebBrowser instance browser as sender...");
            WebBrowser browser = sender as WebBrowser;
            Console.WriteLine("Done.");
            Console.ReadLine();
            string response = browser.DocumentText;

            Console.WriteLine("Searching for authenticity token...");
            // looks in the page source to find the authenticity token.
            // could also use regular expressions here.
            int index = response.IndexOf("authenticity_token");
            int startIndex = index + 41;
            string authenticityToken = response.Substring(startIndex, 40);
            Console.WriteLine("Found authenticity token.");

            Console.WriteLine("Unregistering first event handler...");
            // unregisters the first event handler
            // adds a second event handler
            browser.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(pageLoaded);
            Console.WriteLine("Done.");
            Console.WriteLine("Adding second event handler...");
            browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(pageLoaded2);
            Console.WriteLine("Done.");
            Console.Read();

            Console.WriteLine("Formatting data to be posted to server...");
            string postData = string.Format("_user={0}&_pass={1}&authenticity_token={2}&_task{3}&_action{4}&_timezone{5}", username, password, authenticityToken, task, action, timezone);
            Console.WriteLine("Done.");
            Console.Read();

            Console.WriteLine("Declaring ASCIIEncoding instance enc...");
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            Console.WriteLine("Done.");
            Console.Read();

            //  we are encoding the postData to a byte array
            Console.WriteLine("Encoding postData to a byte array...");
            browser.Navigate(url, "", enc.GetBytes(postData), "Content-Type: application/x-www-form-urlencoded\r\n");
            Console.WriteLine("Done..");
            Console.Read();

        }

        [STAThread]
        static void Main(string[] args)
        {
            Program p = new Program();
            p.Login();
        }
    }
}

From all those console outputs, it only gets to Using method Navigate of browser instance with url parameter…

  • 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-06-04T13:41:09+00:00Added an answer on June 4, 2026 at 1:41 pm

    WebBrowser requires that your program pumps a message loop. It won’t fire its events otherwise. This is in general a requirement for any program that uses a single-threaded COM component. Or to put it in more understandable terms: you can’t get a program to be busy reading from the console and at the same time fire events like DocumentCompleted. A thread can do only one thing at time. You pump a message loop by writing a Winforms app or starting one yourself with Application.Run(). With a message loop, a thread can do more than one thing at a time. But that does require very different code from what you’ve written right now, you still can’t use Console.ReadLine(), you’d use a TextBox instead.

    You can rescue what you have by running the browser in a separate thread, you’ll find the code you need for that in this answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has

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.