C#清除WebBrowser中Cookie缓存的方法
作者:Demon 时间:2022-01-13 20:01:08
本文实例讲述了C#清除WebBrowser中Cookie缓存的方法。分享给大家供大家参考,具体如下:
最近用C#写一个程序,用一个窗体中的WebBrowser来登陆网站,但是WebBrowser有cookie缓存,第二次登陆的时候WebBrowser仍然是第一次登陆后的状态,所以要清除WebBrowser的cookie缓存。
在stackoverflow上找到一段可用的代码:
[DllImport("wininet.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
public static extern bool InternetSetOption(int hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
private unsafe void SuppressWininetBehavior()
{
/* SOURCE: http://msdn.microsoft.com/en-us/library/windows/desktop/aa385328%28v=vs.85%29.aspx
* INTERNET_OPTION_SUPPRESS_BEHAVIOR (81):
* A general purpose option that is used to suppress behaviors on a process-wide basis.
* The lpBuffer parameter of the function must be a pointer to a DWORD containing the specific behavior to suppress.
* This option cannot be queried with InternetQueryOption.
*
* INTERNET_SUPPRESS_COOKIE_PERSIST (3):
* Suppresses the persistence of cookies, even if the server has specified them as persistent.
* Version: Requires Internet Explorer 8.0 or later.
*/
int option = (int)3/* INTERNET_SUPPRESS_COOKIE_PERSIST*/;
int* optionPtr = &option;
bool success = InternetSetOption(0, 81/*INTERNET_OPTION_SUPPRESS_BEHAVIOR*/, new IntPtr(optionPtr), sizeof(int));
if (!success)
{
MessageBox.Show("Something went wrong !>?");
}
}
希望本文所述对大家C#程序设计有所帮助。
标签:C#,WebBrowser,Cookie
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java 中 poi解析Excel文件版本问题解决办法
2023-11-15 16:49:45
浅谈Java中几种常见的比较器的实现方法
2022-04-08 19:04:36
PyQt5内嵌浏览器注入JavaScript脚本实现自动化操作的代码实例
2023-11-26 15:05:59
Springboot基础学习之初识SpringBoot
2022-08-18 08:48:59
![](https://img.aspxhome.com/file/2023/8/75408_0s.png)
androidQ sd卡权限使用详解
2021-09-27 17:38:51
struts2自定义拦截器的示例代码
2021-12-01 16:24:52
![](https://img.aspxhome.com/file/2023/3/78243_0s.jpg)
Java模拟有序链表数据结构的示例
2023-09-26 22:25:30
Java SpringBoot实现AOP
2023-05-31 05:49:30
![](https://img.aspxhome.com/file/2023/1/63001_0s.png)
基于Spring整合mybatis注解扫描是否成功的问题
2023-01-23 18:29:22
![](https://img.aspxhome.com/file/2023/9/71149_0s.png)
JAVA关键字及作用详解
2022-05-03 03:50:14
Android 消息机制详解及实例代码
2023-07-28 08:37:49
![](https://img.aspxhome.com/file/2023/3/85963_0s.png)
详解Eclipse提交项目到GitHub以及解决代码冲突
2022-05-15 09:04:52
![](https://img.aspxhome.com/file/2023/6/78966_0s.png)
java判断字符串是否为数字的方法小结
2023-11-25 05:54:52
利用java操作Excel文件的方法
2021-12-13 03:03:49
Java操作excel的三种常见方法实例
2022-12-11 02:29:55
![](https://img.aspxhome.com/file/2023/2/77362_0s.png)
在maven中引入本地jar包的步骤
2023-11-25 10:43:44
Idea中maven项目实现登录验证码功能
2023-11-29 16:50:51
![](https://img.aspxhome.com/file/2023/5/60575_0s.png)
不使用Math.random方法生成随机数(随机数生成器)
2021-11-28 05:08:46
Java流程控制之循环结构for,增强for循环
2023-11-03 10:45:20
![](https://img.aspxhome.com/file/2023/4/58834_0s.jpg)
SpringBoot的@Value给静态变量注入application.properties属性值
2023-09-20 11:54:07