How to set and delete cookies from WebBrowser Control for arbitrary domains

落爺英雄遲暮 提交于 2019-12-17 16:08:55

问题


How can I set and delete cookies for a domain in webbrowser control without using Javascript (which doesn't allow to set / delete cookies without navigating to the website first.)


回答1:


Managed to accomplish this task by combining these 2:

http://support.microsoft.com/kb/815718

and INTERNET_OPTION_END_BROWSER_SESSION - http://msdn.microsoft.com/en-us/library/windows/desktop/aa385328(v=vs.85).aspx




回答2:


Hope this helps

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("\nPress any key to continue...");
            Console.ReadKey(true);
        }
    }
}

Reference




回答3:


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).




回答4:


Here is a blog post on how do delete cookies in WebBrowser control using wininet.

http://www.alphatecit.com.au/code-snippets/facebook-c-sdk-multiple-login-problem-resolved/




回答5:


Here's a finer solution that only clears cookies (C/C++):

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

All credits to this blog post (C#). Don't forget to check the documentation for InternetSetOption and INTERNET_SUPPRESS_COOKIE_PERSIST




回答6:


You can't delete cookies for a domain other than the domain of the current site.

To do what you are asking requires you have access to the machine (i.e. toolbar installed). Even then it's kludgy.

The only exception is if you are on a domain where the cookie is using the * wildcard, for example *.stackoverflow.com. If you change a cookie with the wildcard, then all the child sub domains (i.e. blog.stackoverflow.com) will have access and see the change that was made to the cookie.



来源:https://stackoverflow.com/questions/1688991/how-to-set-and-delete-cookies-from-webbrowser-control-for-arbitrary-domains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!