简体   繁体   中英

Load web page contents in console application using c#

I want to load contents of below web page in console application using c#. http://justicecourts.maricopa.gov/findacase/casehistory.aspx

Using below code I am getting empty on the screen but it works perfectly if I load google.com web page.

By using WebClient and WebRequest I was getting error "Please enable javascript" and content was not loading so I used below code and javascipt error is not displaying now but web page content is not loading. I am struggling with this issue quite from long time, have seen lot of post regarding this and couldn't get this work.

Could anyone please help?

Thanks in Advance..

class Program
{
    private static bool completed = false;
    private static WebBrowser wb;
    [STAThread]
    static void Main(string[] args)
    {
        wb = new WebBrowser();
        wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
        wb.Navigate("http://justicecourts.maricopa.gov/findacase/casehistory.aspx");
        while (!completed)
        {
            Application.DoEvents();
            Thread.Sleep(100);
        }
        Console.Write("\n\nDone with it!\n\n");
        Console.ReadLine();


    }

    static void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        Console.WriteLine(wb.Document.Body.InnerHtml);
        completed = true;
    }

}

If you literally just want to dump the contents of that URL out to the console, try this:

using(WebClient client = new WebClient()) {
   Console.WriteLine(client.DownloadString(url));
}

try adding more wait.

static void Main(string[] args)
    {
        wb = new WebBrowser();
        wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
        wb.Navigate("http://justicecourts.maricopa.gov/findacase/casehistory.aspx");
        while (!completed)
        {
            Application.DoEvents();
            Thread.Sleep(100);
        }
       //wait even more
       for (int i = 0; i < 6; i++)
        {
            Application.DoEvents();
            Thread.Sleep(1000);
        }
        Console.Write("\n\nDone with it!\n\n");
        Console.ReadLine();


    }

otherwise you can use EO Browser it is paid. but in your case trail will work cause it is not GUI application.as it shows trail message in GUI.

in EO you can say..

EOContorol.WebView.LoadUrlAndWait(URL);

Try using PhantomJs basicaly like running a webbrowser without a window. (headless)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM