WebBrowser的Cookie操作之流量刷新机

最近一直在思考着如何通过代码去伪装或实现人工自然浏览网页的效果,起初能想到的是用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");
复制代码

 注:删除Cookie在测试中一直效果不是很好,至少无法在cnzz统计中刷新到独立访客,当禁止了IE中的Cookie时,IP数与独立访客数就可以同时增长啦。


其实八爪鱼招标网每天正面临着流量的问题,同时百度权重也希望能够在一定时间内得能够提升,所以想做一个好的招标网站,而且还是个免费招标网站是一件非常不容易的事情。

转载请注明出处[ http://samlin.cnblogs.com/]

你可能感兴趣的:(WebBrowser的Cookie操作之流量刷新机)