WebBrowser是.NET中一個十分強大的控件,利用這個控件我們做很多帶有自動功能的工具,如論壇的自動登錄,網頁特定內容的截取,甚至編寫屬於自己的專用瀏覽器,今天主要說說怎麼利用這個控件實現自動點擊廣告的功能。
首先簡單介紹一下WebBrowser組件,下面是摘至MSDN的說明:使用 WebBrowser
控件,可以複製應用程序中的 Internet Explorer Web 瀏覽功能,還可以禁用默認的 Internet Explorer
功能,並將該控件用作簡單的 HTML 文檔查看器。此外,可以使用該控件將基於 DHTML 的用戶界面元素添加到窗體中,還可以隱瞞這些元素在 WebBrowser 控件中承載的事實。通過這種方法,可以將 Web 控件和 Windows 窗體控件無縫地整合到一個應用程序中. 關於這個控件更詳細的介紹,可以參見WebBrowser 控件概述.
說到廣告點擊,大家都不會陌生,最有名的恐怕就是google adsense了,但今天不準備以google adsense爲例子,理由很簡單,google有着非常嚴格的防作弊規則,雖然利用 WebBrowser可以成功點擊,但時間長了難保會被google察覺.今天將以國外一個出名的廣告點擊站bux.to爲例說一下自動點擊功能的實現,bux.to是目前比較火的一個站點,它的註冊用戶每點擊一次廣告將會獲得1美分的收入,是個典型的廣告點擊站.廢話少說,進入我們的正題.
需要實現的功能:
一 賬戶保存功能:只有第一次運行程序時需要填寫用戶名/密碼,以後每次都是自動錄入.
二 自動點擊功能:成功登錄後統計廣告數,自動跳入廣告點擊頁,到了規定的停留時間後進入下一條廣告直到結束.
具體實現:
一 賬戶自動保存
賬戶的保存有很多種實現方法,我這裏使用的是xml文件保存的方式,首先新建一個xml文檔,取名爲userinfo.xml,其結構非常簡單,只有兩個節點.
<userinfo>
<userid>***</userid>
<pwd>***</pwd>
</userinfo>
xmlDoc.Load("userinfo.xml");
if (xmlDoc != null)
{
XmlNode root = xmlDoc.SelectSingleNode("userinfo");
string userid = root.ChildNodes[0].InnerText;
string pwd = root.ChildNodes[1].InnerText;
this.textBoxUserid.Text = userid;
this.textBoxPwd.Text = pwd;
}
else
{
MessageBox.Show("XML文件讀取錯誤!");
}
string pwd = this.textBoxPwd.Text;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("userinfo.xml");
if (xmlDoc != null)
{
XmlNode root = xmlDoc.SelectSingleNode("userinfo");
root.ChildNodes[0].InnerText = userid;
root.ChildNodes[1].InnerText = pwd;
xmlDoc.Save("userinfo.xml");
}
else
{
MessageBox.Show("XML文件寫入錯誤!");
}
首先拖入一個WebBrowser控件,Url屬性設置爲bux.to的登錄地址,雙擊DocumentCompleted事件編寫事件響應程序,即網頁載入結束後執行.核心代碼如下:
HtmlDocument HTMLDocument = this.webBrowser.Document;
if (url.IndexOf("surf.php") > 0)
{
lblCount.Visible = true;
lblTime.Visible = true;
labelCount.Visible = true;
labelTime.Visible = true;
HtmlElementCollection all = HTMLDocument.All;
int k = 0;
for (int i = 0; i < all.Count; i++)
{
HtmlElement elem = all[i];
if (elem.TagName.ToLower() == "a")
{
this.lblCount.Text = k.ToString();
string anchorString = elem.OuterHtml;
if (anchorString.IndexOf("?ad=") > 0)
{
k++;
anchorString = anchorString.Substring(anchorString.IndexOf("?ad=")+4, 5);
string newUrl = "http://www.bux.to/view.php?ad=" + anchorString;
this.webBrowser.Navigate(newUrl);
}
}
}
}
else if (url.IndexOf("view.php") > 0)
{
this.webBrowser.Stop();
this.timerClock.Enabled = true;
}
else if (url.IndexOf("login.php") > 0)
{
string userid = this.textBoxUserid.Text;
string pwd = this.textBoxPwd.Text;
if (this.webBrowser.Document == null)
{
MessageBox.Show("請先打開一個要分析的網頁");
return;
}
HtmlElement tbUserid = this.webBrowser.Document.All["COOKIEusername"];
HtmlElement tbPasswd = this.webBrowser.Document.All["COOKIEpass"];
tbUserid.SetAttribute("value", userid);
tbPasswd.SetAttribute("value", pwd);
}
private void timerClock_Tick(object sender, EventArgs e)
{
if (startTime != 0)
{
startTime--;
this.lblTime.Text = startTime.ToString();
}
else
{
this.timerClock.Enabled = false;
startTime = 35;
this.webBrowser.Navigate("http://www.bux.to/surf.php");
}
}
最後,上一張程序運行時的截圖,爲了達到更好的界面效果,這裏使用了第三方的皮膚組件.