[C #] [ASP] gets AJAX Web pages through WebBrowser.

Source: Internet
Author: User

[C#][asp.net] The Web page after Ajax was obtained through WebBrowser

Today, Shih-min asked me to say that if the Web page starts with Ajax it will load some data, but through WebClient to catch

Catch all is JavaScript and Ajax source code, there is a way to catch AJAX after the value of the data?!

This demand, if the writing crawler may also have this demand. My approach is this:

I am an ASP.

Create a class remember to join System.Windows.Forms

This is when I build a Class called Webbrowsercrawler.

Using System.threading2
Using SYSTEM.WINDOWS.FORMS2
Namespace Getafterajaxpage
{
    public class Webbrowsercrawler
    {
        WebBrowser
        Private WebBrowser _webbrowder2
        Final results
        private string _result {Get2 Set2}
        Url
        private string _path {Get2 Set2}
      
        &lt2summary&gt2
        The method of external openness
        &lt2/summary&gt2
        &lt2param name= "url" &gt2url path&lt2/param&gt2
        &lt2returns&gt2&lt2/returns&gt2
        public string getreult (string url)
        {
            _path = Url2
            var mthread = new Thread (Fatchdatatoresult) 2
            
            The. NET Framework does not use apartment;managed objects to use all shared resources in a secure thread (thread-safe) manner.
            
            
            
            Because a particular thread can only initialize one COM Apartment at a time, the Apartment type cannot be changed after the first call to unmanaged code.
            From:http://msdn.microsoft.com/zh-tw/library/system.threading.apartmentstate.aspx
            Mthread.setapartmentstate (ApartmentState.STA) 2
            Mthread.start (2)
            Mthread.join (2)
            Return _RESULT2
        }
        &lt2summary&gt2
        Call _webbrowder Crawl Data
        For Thread call
        &lt2/summary&gt2
        private void Fatchdatatoresult ()
        {
            
            _webbrowder = new WebBrowser () 2
            _webbrowder.documentcompleted + = _webbrowder_documentcompleted2
            _webbrowder.navigate (_path) 2
            Handles all Windows messages currently in the Message NIN column.
            If you call DoEvents in code, your application can handle other events. For example, if your form adds data to a ListBox and adds DoEvents to the code, the form will be redrawn when another window is dragged onto your form.
            If you remove DoEvents from the code, your form will not be redrawn until the event handler for the button click executes.
            while (_webbrowder.readystate! = webbrowserreadystate.complete)
            {
                Application.doevents (2)
            }
            _webbrowder.dispose (2)
        }
        Backfill after end
        void _webbrowder_documentcompleted (object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            _result = (sender as WebBrowser). Document.Body.InnerHtml2
            
        }
    }
}

Why do you want to write this through the thread to call it or you will encounter

After I call the end:

Webbrowsercrawler  webbrowsercrawler=new Webbrowsercrawler () 2
          File.writealltext (Server.MapPath ("Sample.txt"), Webbrowsercrawler.getreult (http://www.in2.cc/sample/waterfalllab.htm)) 2

Where I was to write the obtained data into Sample.txt &HELLIP2

Where the test page is http://www.in2.cc/sample/waterfalllab.htm

Please note that the test page is for tutoring grade children under 12 years old, please look for parents to accompany to watch

If you go through WebClient, you will see only Javascript call Ajax code, but you can get the data through this method

It would be the data he had called after Ajax, but of course, the Ajax &hellip2 that the Web page was going to call.

But it's not 100%, it's about the Ajax notation &hellip2

---

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.