網(wǎng)頁(yè)自動(dòng)登錄(提交Post內(nèi)容)的用途很多,如驗(yàn)證身份、程序升級(jí)、網(wǎng)絡(luò)投票等,以下是用C#實(shí)現(xiàn)的方法。
網(wǎng)頁(yè)自動(dòng)登錄和提交POST信息的核心就是分析網(wǎng)頁(yè)的源代碼(HTML),在C#中,可以用來(lái)提取網(wǎng)頁(yè)HTML的組件比較多,常用的用WebBrowser、WebClient、HttpWebRequest這三個(gè)。以下就分別用這三種方法來(lái)實(shí)現(xiàn):
1、WebBrowser是個(gè)"迷你"瀏覽器,其特點(diǎn)是Post時(shí)不用關(guān)心Cookie、內(nèi)置JS等問(wèn)題
WebBrowser是VS2005新提供的組件(其實(shí)就是封裝了IE接口),實(shí)現(xiàn)POST功能一般在webBrowser的DocumentCompleted中分析HtmlDocument 來(lái)實(shí)現(xiàn),代碼如下:
HtmlElement ClickBtn =null;
if (e.Url.ToString().ToLower().IndexOf("xxx.htm") > 0) //登陸頁(yè)面
{
HtmlDocument doc = webBrowser1.Document;
for (int i = 0; i < doc.All.Count ; i++)
{
if (doc.All[i].TagName.ToUpper().Equals("INPUT"))
{
switch (doc.All[i].Name)
{
case "userCtl":
doc.All[i].InnerText = "user01";
break;
case "passCt1":
doc.All[i].InnerText = "mypass";
break;
case "B1":
ClickBtn = doc.All[i]; //提交按鈕
break;
}
}
}
ClickBtn.InvokeMember("Click"); //執(zhí)行按扭操作
}
2、WebClient封裝了HTTP的一些類,操作簡(jiǎn)單,相較于webBrowser,特點(diǎn)是可以自設(shè)代理,缺點(diǎn)是對(duì)COOKIE的控制
WebClient的運(yùn)行全在后臺(tái),并且提供了異步操作的能力,這樣很方便并發(fā)多個(gè)任務(wù),然后等待結(jié)果的返回,再逐個(gè)處理。多任務(wù)異步調(diào)用的代碼如下:
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 OpenReadCompletedEventHandler(Pic_OpenReadCompleted2);
wcArray[idArray].UploadDataCompleted += new UploadDataCompletedEventHandler(Pic_UploadDataCompleted2);
try
{
......
wcArray[idArray].Proxy = new WebProxy(proxy[1], port);
wcArray[idArray].OpenReadAsync(new Uri("http://xxxx.com.cn/tp.asp?Id=129")); //打開(kāi)WEB;
proxy = null;
}
catch
{
}
}
}
private void Pic_OpenReadCompleted2(object sender, OpenReadCompletedEventArgs 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); // 轉(zhuǎn)化成二進(jìn)制數(shù)組
((WebClient)sender).UploadDataAsync(new Uri("http://xxxxxxy.com.cn/tp.asp?Id=129"), "POST", byteArray);
}
}
private void Pic_UploadDataCompleted2(object sender, UploadDataCompletedEventArgs e)
{
if (e.Error == null)
{
string returnMessage = Encoding.Default.GetString(e.Result);
......
}
}
3、HttpWebRequest較為低層,能實(shí)現(xiàn)的功能較多,Cookie操作也很簡(jiǎn)單
private bool PostWebRequest()
{
CookieContainer cc = new CookieContainer();
string pos tData = "user=" + strUser + "&pass=" + strPsd;
byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 轉(zhuǎn)化
HttpWebRequest webRequest2 = (HttpWebRequest)WebRequest.Create(new Uri("http://www.xxxx.com/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); //寫(xiě)入?yún)?shù)
newStream.Close();
HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();
StreamReader sr2=new StreamReader(response2.GetResponseStream(), Encoding.Default);
string text2 = sr2.ReadToEnd();
......
}
HttpWebRequest同樣提供了異步操作,有興趣的朋友自己查MSDN,實(shí)現(xiàn)起來(lái)也不難。
作者:Gezidan
本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁(yè)面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。
本文轉(zhuǎn)載自 http://www.cnblogs.com/xiangboren/archive/2009/03/06/1404473.html
posted on 2011-09-28 13:40
日需博客 閱讀(940)
評(píng)論(0) 編輯 收藏 引用 所屬分類:
C# 、
技術(shù)文章 、
轉(zhuǎn)載