在winform窗体上,放一个WebBrowser控件,就行了。
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlDocument doc = webBrowser1.Document;
HtmlElement btn = null;
foreach (HtmlElement em in doc.All)
{
string str = em.Name;
if (str.Length < 1)
{
str = em.Id;
}
if ((str == "username") || (str == "password") || (str == "登录邮箱"))
{
switch (str)
{
case "username": em.SetAttribute("value", "liukuo.lk"); break;
case "password": em.SetAttribute("value", "123456"); break;
case "登录邮箱": btn = em; break;
default: break;
}
}
}
btn.InvokeMember("click");
doc.Forms["login163"].InvokeMember("登录邮箱");
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://mail.163.com/");
}
}
}
用户名和密码要写成你自己的邮箱用户名和密码,这样才能登陆邮箱。
完成程序下载
webBrowser1执行js:
HtmlDocument doc = webBrowser1.Document;
doc.InvokeScript("submitForm", new object[] { });
webBrowser1增加post数据:
HtmlElement add = doc.CreateElement("<input name='bssCredentials.operatorId' value='" + value + "' />");
doc.Forms["loginForm"].AppendChild(add);
查看Post数据
引用system32下SHDocVw.dll
Form_Load()里面:
SHDocVw.WebBrowser wb = (SHDocVw.WebBrowser)webBrowser1.ActiveXInstance;
wb.BeforeNavigate2 += new DWebBrowserEvents2_BeforeNavigate2EventHandler(WebBrowser_BeforeNavigate2);
private void WebBrowser_BeforeNavigate2(object pDisp, ref object URL, ref object Flags, ref object TargetFrameName, ref object PostData, ref object Headers, ref bool Cancel)
{
string postDataText = System.Text.Encoding.ASCII.GetString(PostData as byte[]);
}