(This blog post is purely a personal record. Please indicate the source for reprinting !)
Official Address:NHtmlUnit
Dll compilation method:
1. Download nuget.exe (nuget Website: http://www.nuget.org /)
2. Open the doscommand dialog box,
Directory where the nuget.exe file is located (My exe file is placed on the desktop)
3. Install NHtmlUnit. After installation, two folders will be created in the current directory. One is to install the required environment IKVM, and the other is the required NHtmlUnit folder, which contains the generated dll under the lib directory.
Run the command: nuget install NHtmlUnit
3. You can go to the official HtmlUnit website to read the document or search for tutorials in the blog Garden. The usage of HtmlUnit is the same as that of JAVA. NHtmlUnit is only
Add a shell to HtmlUnit so that. net can call JAVA.
Instance code (reference dll: HtmlUnit, NHtmlUnit. To be lazy, I have referenced all the IKVM dll files ):
class Program
{
static void Main(string[] args)
{
WebClient client = new WebClient();
client.Options.JavaScriptEnabled = false;
client.Options.CssEnabled = true;
client.Options.ThrowExceptionOnScriptError = false;
client.Options.Timeout = 5000;
HtmlPage page = client.GetHtmlPage("http://www.baidu.com/#wd=11&rsv_spt=1&issp=1&rsv_bp=0&ie=utf-8&tn=baiduhome_pg&rsv_sug3=2&rsv_sug2=0&inputT=26");
Thread.Sleep(3000);
string xml = page.AsXml();
HtmlDocument doc = new HtmlDocument();//忽略此类,这个类是其他DLL提供的,与NHtmlUnit无关
doc.LoadHtml(xml);
string nodeName = doc.GetElementbyId("container").Name;
Console.WriteLine("\r\n\r\n\r\n\r\n" + nodeName + "\r\n\r\n\r\n\r\n" );
Console.Read();
}
}