Surfing the Web in a Console App

0

I try to create a Web Browser object from a Console application, here what I try to do but I get an error:

static void Main(string[] args)
    {
        WebBrowser web = new WebBrowser();

        web.Navigate("www.es.stackoverflow.com");

        Console.WriteLine("Funcionando Correctamente");
    }
  

Unhandled exception of type 'System.Threading.ThreadStateException' in System.Windows.Forms.dll

    
asked by Diego 29.04.2017 в 18:42
source

1 answer

2

WebBrowser is part of Windows forms so you should not use it in a console application if you want to "browse the web from the console" use Net :

static void Main(string[] args)
        {

            System.Net.WebRequest req = System.Net.WebRequest.Create("http://es.stackoverflow.com/");
            System.Net.WebResponse resp = req.GetResponse();
            System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
            Console.WriteLine(sr.ReadToEnd().Trim());
            Console.ReadKey();
        }
    
answered by 29.04.2017 / 20:31
source