清除Web浏览器Cookie

清除Web浏览器Cookie

本文介绍了清除Web浏览器Cookie Winforms C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何清除Web浏览器控件Winforms C#的cookie,有什么方法可以解决Winforms Web浏览器控件中的问题

How I can clear cookies for web browser control winforms C# , is there any way to clear cookies problematically in winforms web browser control

推荐答案

您可以在导航到站点之前禁用缓存(包括cookie)。为此,可以使用 API函数并将 INTERNET_OPTION_SUPPRESS_BEHAVIOR(81)选项的值设置为 INTERNET_SUPPRESS_COOKIE_PERSIST(3 )值。

You can disable cache (including cookies) before navigating to the site. To do so, you can use InternetSetOption API function and set the value of INTERNET_OPTION_SUPPRESS_BEHAVIOR(81) option to INTERNET_SUPPRESS_COOKIE_PERSIST(3) value.

示例

以下例如,工作方式类似于开始新的会话。虽然我在计算机上登录了 outlook.com ,但是当我打开此应用程序并浏览 outlook.com 禁用cookie和缓存后,它的工作方式类似于开始一个新会话,我需要登录到outlook.com:

The following example, works like starting a new session. While I've logged in to the outlook.com on my machine, but when I open this application and browse outlook.com after disabling cookies and cache, it works like starting a new session and I need to login to outlook.com:

//using System.Runtime.InteropServices;
[DllImport("wininet.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption,
    IntPtr lpBuffer, int dwBufferLength);
private void Form1_Load(object sender, EventArgs e)
{
    var ptr = Marshal.AllocHGlobal(4);
    Marshal.WriteInt32(ptr, 3);
    InternetSetOption(IntPtr.Zero, 81, ptr, 4);
    Marshal.Release(ptr);
    webBrowser1.Navigate("https://outlook.com");
}

要查找有关这些标志的更多信息,请查看。

To find more information about these flags, take a look at Windows Internet Option Flags.

注意:在我的另一篇文章中,,您可以找到此答案的VB.NET版本。

Note: You can find a VB.NET version of this answer, here in my other post.

这篇关于清除Web浏览器Cookie Winforms C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 03:05