基本信息
源码名称:webbrowser 设置cookie 以及清除cookie 例子,附源码
源码大小:3.17KB
文件格式:.txt
开发语言:C#
更新时间:2013-08-02
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
先附上删除cookie例子
// 清除所有COOKIE using System; using System.IO; void clearIECache() { ClearFolder (new DirectoryInfo (Environment.GetFolderPath(Environment.SpecialFolder.InternetCache))); } void ClearFolder (DirectoryInfo folder) { foreach (FileInfo file in folder.GetFiles()) { file.Delete(); } foreach (DirectoryInfo subfolder in folder.GetDirectories()) { ClearFolder(subfolder); } }
下面是设置cookie方法
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; using System.Text.RegularExpressions; using Common; using mshtml; namespace spider { public partial class WininetTest : Form { /// <summary> /// 获取cookie /// </summary> [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool InternetGetCookie(string url, string name, StringBuilder data, ref int dataSize); /// <summary> /// 设置cookie /// </summary> [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData); public WininetTest() { InitializeComponent(); } private void WininetTest_Load(object sender, EventArgs e) { //string url = "http://wenwen.soso.com/"; string url = "http://zhidao.baidu.com/"; // string url = "http://weibo.com/"; this.webBrowser.Navigate(url); } private void btnGetCookie_Click(object sender, EventArgs e) { this.txtCookie.Text = GetCookie(); } private void btnSetCookie_Click(object sender, EventArgs e) { ////删除旧的 foreach (string fileName in System.IO.Directory.GetFiles(System.Environment.GetFolderPath(Environment.SpecialFolder.Cookies))) { if (fileName.ToLower().IndexOf("zhidao") > 0) { System.IO.File.Delete("zhidao"); } // if (fileName.ToLower().IndexOf("soso") > 0) // { // System.IO.File.Delete("soso"); // } } //生成新的 foreach (string c in GetCookie().Split(';')) { string[] item = c.Split('='); if (item.Length == 2) { string name = item[0]; string value = item[1] ";expires=Sun,22-Feb-2099 00:00:00 GMT"; InternetSetCookie(webBrowser.Url.ToString(), name, value); this.txtNewCookie.Text = name "=" value ";"; } } } public string GetCookie() { //获取旧的 StringBuilder cookie = new StringBuilder(new String(' ', 2048)); int datasize = cookie.Length; bool b = InternetGetCookie(webBrowser.Url.ToString(), null, cookie, ref datasize); if (b) { return webBrowser.Document.Cookie; } return null; } private void btnSave_Click(object sender, EventArgs e) { string cookie = this.txtNewCookie.Text; } } }