English 中文(简体)
如何为任意领域建立和删除网络浏览器控制中的 co
原标题:How to set and delete cookies from WebBrowser Control for arbitrary domains

我如何在网络浏览器控制领域安装和删除 co(,允许在不浏览网站的情况下建立/删除 co。

最佳回答
问题回答

希望这一帮助

using System.Runtime.InteropServices;

namespace Storm8
{
    class Program
    {

        [DllImport("wininet.dll", SetLastError = true)]
        private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);

        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool InternetGetCookie(
            string lpszUrlName,
            string lpszCookieName,
            StringBuilder lpszCookieData,
            [MarshalAs(UnmanagedType.U4)]
            ref int lpdwSize
        );

        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool InternetSetCookie(
            string lpszUrlName,
            string lpszCookieName,
            string lpszCookieData
        );

        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool InternetSetOption(
            int hInternet,
            int dwOption,
            string lpBuffer,
            int dwBufferLength
        );



        [STAThread]
        static void Main(string[] args)
        {
            InternetSetOption(0, 42, null, 0);
            InternetSetCookie("http://domain.name.com", "cookiename", "cookievalue");

            WebBrowser wb = new WebBrowser();
            string testUrl = "http://domain.name.com/fight.php?showAttackBg=true";
            string additionalHeaders = "User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit /528.18 (KHTML, like Gecko) Mobile/7A341" + Environment.NewLine +
                "Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" + Environment.NewLine +
                "Accept-Language: en-gb";

            if (wb.Document == null)
                wb.Navigate(testUrl, null, null, additionalHeaders);

            while (wb.Document == null)
                Application.DoEvents();

            Console.WriteLine("
Press any key to continue...");
            Console.ReadKey(true);
        }
    }
}

IE Uses WinInet functions for networking so you can use WinInet s cookie functions to change the cookie. Update: The requirement demands per-process setting. Since the cache folder location is not stored in IE settings registry key IDocHostUIHandler2::GetOverrideKeyPath won t work. I don t know a way to customize the cookie folder location at the process level except to hook all WinInet APIs (and stuck with updating application to accommodate future WinInet APIs).

这里的微额解决办法是,只清除 co(C/C++):

#include <wininet.h>
#include <winineti.h>
...
DWORD dwSuppress = INTERNET_SUPPRESS_COOKIE_PERSIST;
InternetSetOption(0, INTERNET_OPTION_SUPPRESS_BEHAVIOR, &dwSuppress, sizeof(DWORD));

http://mdb-blog.blogspot.fr/02/c-winforms-webbrowser-clear-all-cookies.html (C#)。 Don tabes toeck the documentation for ;InternetSetOption





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.