最近一直在思考着如何通過代碼去偽裝或實現人工自然瀏覽網頁的效果,起初能想到的是用WebBrowser實現這一效果,需要達到的功能預想有以下幾點:
1、自動刷新
2、模擬人工下拉滾動條並停留一段時間;
3、可以刷IP、刷流量;
4、可以增加任意來訪域名;
5、自動隨機點擊站內頁面;
6、自動隨機點擊站內廣告;
7、自動清除Cookie、Session;
8、能夠模擬搜索引擎搜索關鍵字並點擊到指點頁面;
一、WebBrowser中獲取Cookie生成CookieContainer
1.在WebBrowser中獲取Cookie CookieContainer myCookieContainer = new CookieContainer(); string cookieStr = webBrowser1.Document.Cookie; string[] cookstr = cookieStr.Split(';'); foreach (string str in cookstr) { string[] cookieNameValue = str.Split('='); Cookie ck = new Cookie(cookieNameValue[0].Trim ().ToString(), cookieNameValue[1].Trim ().ToString()); ck.Domain = "www.google.com"; myCookieContainer.Add(ck); } WebClient設置cookie! WebClient wc = new WebClient(); wc.Headers.Add("Cookie", "PHPSESSID=" + cookie + ";"); // 注意,這里是Cookie,不是Set-Cookie byte[] re = wc.UploadData(Global.RootPath + "test.php", new byte[0]); System.Text.UTF8Encoding converter = new System.Text.UTF8Encoding(); string str = converter.GetString(re); 2. 在WebBrowser中設置Cookie public partial class WebBrowserControl : Form { private String url; [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData); public WebBrowserControl(String path) { this.url = path; InitializeComponent(); // set cookie InternetSetCookie(url, "JSESSIONID", Globals.ThisDocument.sessionID); // navigate webBrowser.Navigate(url); } } 3.將WebBrowser的cookie信息傳給HttpWebRequest 先建一個"CookieContainer" 把WebBrowser中的Cookie保存在里面 //在WebBrowser中登錄cookie保存在WebBrowser.Document.Cookie中 CookieContainer myCookieContainer = new CookieContainer(); //String 的Cookie 要轉成 Cookie型的 並放入CookieContainer中 string cookieStr = webBrowser1.Document.Cookie; string[] cookstr = cookieStr.Split(';'); foreach (string str in cookstr) { string[] cookieNameValue = str.Split('='); Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString()); ck.Domain = "www.abc.com";//必須寫對 myCookieContainer.Add(ck); } HttpWebRequest hreq = (HttpWebRequest)HttpWebRequest.Create("http://www.abc.com/search.asp"); hreq.Method = "POST"; hreq.ContentType = "application/x-www-form-urlencoded"; //自己創建的CookieContainer hreq.CookieContainer = myCookieContainer; string postdata = "id=2005&action=search&name="; byte[] byte1 = Encoding.ASCII.GetBytes(postdata); hreq.ContentLength = byte1.Length; Stream poststream = hreq.GetRequestStream(); poststream.Write(byte1, 0, byte1.Length); poststream.Close(); HttpWebResponse hres = (HttpWebResponse)hreq.GetResponse();
二、Webbrowser清除Cookie及session
//方法一:調用 wininet.dll清除cookie (推薦) SuppressWininetBehavior(); //方法二:刪除用戶登錄后的信息,這里相當於瀏覽器的注銷功能,使用的是ie自帶的功能 (推薦) HtmlDocument document = wb.Document; document.ExecCommand("ClearAuthenticationCache", false, null); //方法三:刪除本機cookie 此方法會彈出ie清除cookie的彈出框 //Temporary Internet Files (Internet臨時文件) //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8 //Cookies //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2 //History (歷史記錄) //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1 //Form. Data (表單數據) //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 16 //Passwords (密碼) //RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 32 //Delete All (全部刪除) //ShellExecute(IntPtr.Zero, "open", "rundll32.exe", " InetCpl.cpl,ClearMyTracksByProcess 2", "", ShowCommands.SW_HIDE); ShellExecute(IntPtr.Zero, "open", "rundll32.exe", " InetCpl.cpl,ClearMyTracksByProcess 255", "", ShowCommands.SW_HIDE); //方法四:使用webbrowser自帶的清coookie的方法 (不推薦,清不掉session,實測無效) wb.Document.Cookie.Remove(0, (wb.Document.Cookie.Count() - 1)); //方法五:使用js清除cookie (不推薦,清不掉session) wb.Navigate("javascript:void((function(){var a,b,c,e,f;f=0;a=document.cookie.split('; ');for(e=0;e<a.length&&a[e];e++){f++;for(b='.'+location.host;b;b=b.replace(/^(?:%5C.|[^%5C.]+)/,'')){for(c=location.pathname;c;c=c.replace(/.$/,'')){document.cookie=(a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString());}}}})())"); //var a,b,c,e,f; //f=0; //a=document.cookie.split('; '); //b='.'+'baidu.com'; ////b='.'+'www.baidu.com'; //for(e=0;e<a.length;e++){ // //b='.'+location.host; // b=b.replace(/^(?:%5C.|[^%5C.]+)/,''); // c=location.pathname; // c=c.replace(/.$/,''); // ck = a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString(); // console.log(ck); // document.cookie=ck; //} //方法六:使用InternetSetCookie給cookie賦null值 (不推薦) //也可以給此Cookie賦空值:InternetSetCookie //InternetSetCookie("http://.qq.com/", NULL, "uin=; PATH=/; DOMAIN=qq.com");
其中方法三中的ClearMyTracksByProcess 可進行選擇設置 :
Temporary Internet Files (Internet臨時文件)
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8
Cookies
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2
History (歷史記錄)
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1
Form. Data (表單數據)
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 16
Passwords (密碼)
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 32
Delete All (全部刪除)
RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255
注:刪除Cookie在測試中一直效果不是很好,至少無法在cnzz統計中刷新到獨立訪客,當禁止了IE中的Cookie時,IP數與獨立訪客數就可以同時增長啦。