程序员人生 网站导航

WebBrowser的Cookie操作之流量刷新机

栏目:互联网时间:2016-06-12 16:22:35

最近1直在思考着如何通过代码去假装或实现人工自然阅读网页的效果,起初能想到的是用WebBrowser实现这1效果,需要到达的功能料想有以下几点:

1、自动刷新

2、摹拟人工下拉转动条并停留1段时间;

3、可以刷IP、刷流量;

4、可以增加任意来访域名;

5、自动随机点击站内页面;

6、自动随机点击站内广告;

7、自动清除Cookie、Session;

8、能够摹拟搜索引擎搜索关键字并点击到指导页面;

 

 

1、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

先建1个"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();


复制代码

 

2、Webbrowser清除Cookie及session

复制代码
 //方法1:调用 wininet.dll清除cookie (推荐)
            SuppressWininetBehavior();

            //方法2:删除用户登录后的信息,这里相当于阅读器的注销功能,使用的是ie自带的功能 (推荐)
            HtmlDocument document = wb.Document;
            document.ExecCommand("ClearAuthenticationCache", false, null);

            //方法3:删除本机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);


            //方法4:使用webbrowser自带的清coookie的方法 (不推荐,清不掉session,实测无效)
            wb.Document.Cookie.Remove(0, (wb.Document.Cookie.Count() - 1));

            //方法5:使用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()⑴e11).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()⑴e11).toGMTString();
            //    console.log(ck);
            //    document.cookie=ck;
            //}

            //方法6:使用InternetSetCookie给cookie赋null值 (不推荐)
            //也能够给此Cookie赋空值:InternetSetCookie
            //InternetSetCookie("http://.qq.com/", NULL, "uin=; PATH=/; DOMAIN=qq.com");
复制代码

 注:删除Cookie在测试中1直效果不是很好,最少没法在cnzz统计中刷新到独立访客,当制止了IE中的Cookie时,IP数与独立访客数就能够同时增长啦。


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

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

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐