有几个方法,我给你一个最土的,用webbrowser,你实例一个webbrowser对象即可,不需要拖控件,之后去抓取对应网页的HTML,模拟表单事件提交即可。
百度一下C#模拟表单提交,相信你会有收获。
using System; using System.Collections.Generic; using using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using WriteAndReadWebpage; namespace FirstWebBrower { public partial class FrmWenBrowser : Form { // 应用程序根目录 private readonly string executePath; // HTML文件名称 private readonly string htmlFileName; // 读取网页实体 private WebPageControl wpc; public FrmWenBrowser() { InitializeComponent(); // 初始化 executePath = System.AppDomain.CurrentDomain.BaseDirectory; htmlFileName = executePath + "WebPageDemo.html"; //htmlFileName = "C:\\Program Files\\Foxmail\\SMIMEHELP\\ENCHELP.html"; wpc = new WebPageControl(this.webBrowser); this.webBrowser.AllowWebBrowserDrop = false; this.webBrowser.IsWebBrowserContextMenuEnabled = false; this.webBrowser.WebBrowserShortcutsEnabled = false; this.cmbNameOrID.SelectedIndex = 0; // Event register this.Load += new EventHandler(FrmWenBrowser_Load); this.btnGetText.Click += new EventHandler(btnGetText_Click); this.btnWrite.Click += new EventHandler(btnWrite_Click); this.btnCommit.Click += new EventHandler(btnCommit_Click); // Web Navigate this.btnFoward.Click += new EventHandler(btnFoward_Click); this.btnBackward.Click += new EventHandler(btnBackward_Click); this.btnStop.Click += new EventHandler(btnStop_Click); this.btnRefresh.Click += new EventHandler(btnRefresh_Click); } // Handling // Window Create void FrmWenBrowser_Load(object sender, EventArgs e) { // 加载html文件 & 打开网址 this.webBrowser.Navigate(htmlFileName); } // 单击按钮Get响应 void btnGetText_Click(object sender, EventArgs e) { try { if (this.txtAttributeName.Text != string.Empty) { this.txtText.Text = this.cmbNameOrID.Text == "By Name" ? this.wpc.ReadText(this.txtAttributeName.Text) : this.wpc.ReadTextByID(this.txtAttributeName.Text); } } catch { MessageBox.Show("读取控件文本失败,请检查网页中是否包含" + this.txtAttributeName.Text + "控件!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } //单击按钮Write响应 void btnWrite_Click(object sender, EventArgs e) { try { if (this.txtAttributeName.Text != string.Empty) { this.wpc.WriteText(this.txtAttributeName.Text, this.txtText.Text); } } catch { MessageBox.Show("读取控件文本失败,请检查网页中是否包含" + this.txtAttributeName.Text + "控件!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } // 单击按钮Commit响应 void btnCommit_Click(object sender, EventArgs e) { try { //if (this.txtAttributeName.Text != string.Empty) //{ // this.wpc.WriteText(this.txtAttributeName.Text, this.txtText.Text); //} if (this.cmbNameOrID.Text == "By Name") { this.wpc.ClickButton("btnCommit"); } else { //this.wpc.ClickButtonByID("btnContinue"); } } catch { MessageBox.Show("读取控件文本失败,请检查网页中是否包含" + this.txtAttributeName.Text + "控件!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } // 网站导航:向前 void btnFoward_Click(object sender, EventArgs e) { this.webBrowser.GoForward(); } // 网站导航:向后 void btnBackward_Click(object sender, EventArgs e) { this.webBrowser.GoBack(); } // 网站导航:停止 void btnStop_Click(object sender, EventArgs e) { this.webBrowser.Stop(); } // 网站导航:刷新 void btnRefresh_Click(object sender, EventArgs e) { this.webBrowser.Refresh(); } private void btnImg_Click(object sender, EventArgs e) { FrmImage img = new FrmImage(); HtmlElement imgEle = this.webBrowser.Document.GetElementById("imgIBM"); HtmlElement imgNew = img.wb.Document.CreateElement("IMG"); imgNew.SetAttribute("src", imgEle.GetAttribute("src")); //img.wb.Document.Body.AppendChild(imgNew); img.ShowDialog(); } } }