t’s surprisingly easy to get HtmlUnit, a Java library, converted into a native .NET assembly (no Java Virtual Machine needed!) using IKVM.
First, download HtmlUnit (as a compiled JAR file) from SourceForge (I’m using version 2.7), and extract all its files from the ZIP archive.
Second, download IKVM binaries from ikvm.net/SourceForge (I’m using version 0.42.0.3), and again extract all its files from the ZIP archive.
Open a command prompt, ensure you’ve added IKVM’s /bin folder to your PATH variable, change directory to HtmlUnit’s/lib folder, and then run ikvmc to convert the Java bytecode of all of the HtmlUnit JAR files into .NET bytecode. I ran this command:
ikvmc -out:htmlunit-2.7.dll *.jar
你就可以使用 .net,引用强大的HtmlUnit
测试 winformusing System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using com.gargoylesoftware.htmlunit; using com.gargoylesoftware.htmlunit.html; namespace htmlunit { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { WebClient webClient = new WebClient(BrowserVersion.FIREFOX_17); webClient.setAjaxController(new NicelyResynchronizingAjaxController()); webClient.getOptions().setJavaScriptEnabled(true); webClient.getOptions().setActiveXNative(false); webClient.getOptions().setCssEnabled(false); webClient.getOptions().setThrowExceptionOnScriptError(false); var rootPage = webClient.getPage("http://tt.mop.com/read_14304066_1_0.html"); webClient.waitForBackgroundJavaScript(10000); this.richTextBox1.Text = rootPage.getWebResponse().getContentAsString(); } } }