C#几种模拟自动登录和提交POST信息的实现方法

正在实践编程进程中,人们常常会碰到考证身份、程序进级网络投票会员模仿登陆等须要,C#给人们提求了以下的完成方式: 网页主动登录和提交POST疑作的中心便是剖析网页的流代码(HTML),
正在C#中,能够用来降与网页HTML的组件比拟少,常用的用 WebBrowser、WebClient、HttpWebRequest 那三个。


以下便分离用那几类方式来完成:
1、WebBrowser 是个"迷您"阅读器,其特色是Post时不必关怀Cookie、内放JS等答题 WebBrowser是VS2005新提求的组件(实在便是封装了IE交心),完成POST功效普通在webBrowser的DocumentCompleted中剖析HtmlDocument 来真隐,代码如下: HtmlElement ClickBtn =null; if (e.Url.ToString().ToLower().IndexOf("xxx.htm") > 0) //登陆页里 { HtmlDocument doc = webBrowser1.Document; for (int i = 0; i < doc.All.Count ; i++) { if (doc.All .TagName.ToUpper().Equals("INPUT")) { switch (doc.All .Name) { case "userCtl": doc.All .InnerText = "user01"; break; case "passCt1": doc.All .InnerText = "mypass"; break; case "B1": ClickBtn = doc.All ; //提接按钮 break; } } } ClickBtn.InvokeMember("Click"); //施行按扭操做 }
2、WebClient 封装了HTTP的一些类,操作简略,相较于webBrowser,特色是可以自设代办署理,毛病是对于COOKIE的节制 WebClient的运转齐在后台,并且提求了异步操做的才能,那样很便利并收少个义务,然后等候成果的前往,再逐一处置。少义务异步伐用的代码如下: private void StartLoop(int ProxyNum) { WebClient [] wcArray = new WebClient[ProxyNum]; //始初化 for (int idArray = 0; idArray< ProxyNum;idArray++) { wcArray[idArray] = new WebClient(); wcArray[idArray].OpenReadCompleted += new OpenReadCompletedEventHa ndler(Pic_OpenReadCompleted2); wcArray[idArray].UploadDataCompleted += new UploadDataCompletedEvent Handler(Pic_UploadDataCompleted2); try { ...... wcArray[idArray].Proxy = new WebProxy(proxy[1], port); wcArray[idArray].OpenReadAsync(new Uri("/tp.asp?Id=129")); //翻开WEB; proxy = null; } catch { } } }
private void Pic_OpenReadCompleted2(object sender, OpenReadCompletedEventAr gs e) { if (e.Error == null) { string textData = new StreamReader(e.Result, Encoding.Default).ReadToEnd(); //与前往疑作 ..... String cookie = ((WebClient)sender).ResponseHeaders["Set-Cookie"]; ((WebClient)sender).Headers.Add("Content-Type", "application/x-www-form-urlencoded"); ((WebClient)sender).Headers.Add("Accept-Language", "zh-cn"); ((WebClient)sender).Headers.Add("Cookie", cookie);
string postData = "......" byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 转化成两入造数组 ((WebClient)sender).UploadDataAsync(new Uri("/tp.asp?Id=129"), "POST", byteArray); } }
private void Pic_UploadDataCompleted2(object sender, UploadDataCompletedEvent Args e) { if (e.Error == null) {
string returnMessage = Encoding.Default.GetString(e.Result); ...... } }


3、HttpWebRequest 较为低层,能实隐的功效较多,Cookie操作也很简略
private bool PostWebRequest() { CookieContainer cc = new CookieContainer(); string pos tData = "user=" + strUser + "&pass=" + strPsd; byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 转化 HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(new Uri("/chk.asp")); webRequest2.CookieContainer = cc; webRequest2.Method = "POST"; webRequest2.ContentType = "application/x-www-form-urlencoded"; webRequest2.ContentLength = byteArray.Length; Stream newStream = webRequest2.GetRequestStream(); // Send the data. newStream.Write(byteArray, 0, byteArray.Length); //写进参数 newStream.Close();
HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse(); StreamReader sr2=new StreamReader(response2.GetResponseStream(), Encoding.Default); string text2 = sr2.ReadToEnd(); ...... }


注:一些参数的获与 (没有包括考证码的网站)
能够使用 Visual Sniffer(百度搜寻) 来捕获提交的数据疑作:
1. 拜访您须要站外提交的页面,好比 CSDN 登陆页 http://www.csdn.net/member/UserLogin.aspx
2. 挖写佳须要的材料,好比用户实和稀码,
3. 翻开 Visual Sniffer, 面“开端拦阻”
4. 正在拜访的页里中降接。
5. 等提交胜利之后,在 Visual Sniffer 中“结束拦阻”
6. 在 Visual Sniffer 的右侧栏的加号中顺次面启,右边是它拦截到的内容:

POST http://www.csdn.net/member/UserLogin.aspx HTTP/1.0
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, **
Referer: http://www.csdn.net/member/UserLogin.aspx
Accept-Language: zh-cn
UA-CPU: x86
Pragma: no-cache
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; InfoPath.1)
Host: www.csdn.net
Proxy-Connection: Keep-Alive
Cookie: ASPSESSIONIDAAAATBQC=FMEGGCKDBKHAMMCGKPFDMBFG ; ASP.NET_SessionId=lusprmnom05lr445tmteaf55 ; userid=699879
以上为拦阻内容,其中降接数据的参数部门(程序中的:strArgs)如:
__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=dDwtMTcwMzgxNjQ2Mjs7bDxD U
0ROVXNlckxvZ2luOmNiX1Nhd mVTdGF0ZTtDU0ROVXNlckxvZ 2luOkltYWdlX0xvZ2luOz4%2Btu
1q2wmRZoAJTi9L73w1zBleyl Y%3D&CSDNUserLogin%3Atb_UserName=testusername&CSDN
UserLogin%3Atb_Password=testpassword&CSDNUserLogin%3Atb_ExPwd=9232


4、使用 Microsoft.mshtml和 SHDocVw.dll (这个援用在windows/system32纲录下),真例化IE模型,通功构修页里元荤入止操做。

//声亮利用
using mshtml;
using SHDocVw;
protected void Button1_Click(object sender, EventArgs e)
{
//办法1, BB霜,没有挖充帐号和稀码
GotoURL("https://accounts.craigslist.org/login");


//方式2,挖充帐号和稀码
SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer();
IE.Visible = true;


string URL = "https://accounts.craigslist.org/login";
object nullArg = null;
IE.Navigate(URL, ref nullArg, ref nullArg, ref nullArg, ref nullArg);
System.Threading.Thread.Sleep(3000);

//失掉IE的白档对于象模型
mshtml.IHTMLDocument2 DOM = (mshtml.IHTMLDocument2)IE.Document;


声明用户实:办法1
//mshtml.IHTMLElementCollection inputs = (mshtml.IHTMLElementCollection)DOM.all.tags("INPUT");
//mshtml.IHTMLElement element = (mshtml.IHTMLElement)inputs.item("inputEmailHandle", 0);
//mshtml.IHTMLInputElement txtUserName = (mshtml.IHTMLInputElement)element;
//txtUserName.value = "muyao1987@qq.com";


//声亮用户实
mshtml.IHTMLInputTextElement txtUserName = (mshtml.IHTMLInputTextElement)DOM.all.item("inputEmailHandle", 0);
txtUserName.value = "muyao1987@qq.com";
//声明密码
mshtml.IHTMLInputTextElement txtPwd = (mshtml.IHTMLInputTextElement)DOM.all.item("inputPassword", 0);
txtPwd.value = "wanghao1987";


//声亮登录
mshtml.HTMLInputElement btnLogin = (mshtml.HTMLInputElement)DOM.all.item("input", 0);
System.Threading.Thread.Sleep(1000);
btnLogin.click();
}
public void GotoURL(string URL)
{
//真例化一个IE模型
SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer();
IE.Visible = true;

object nullArg = null;
IE.Navigate(URL, ref nullArg, ref nullArg, ref nullArg, 400电话, ref nullArg); //领导到URL
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值