[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}
<2summary>2
The method of external openness
<2/summary>2
<2param name= "url" >2url path<2/param>2
<2returns>2<2/returns>2
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
}
<2summary>2
Call _webbrowder Crawl Data
For Thread call
<2/summary>2
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
---