Setting a cookie in a WebBrowser control

前端 未结 2 394
清歌不尽
清歌不尽 2020-11-30 06:58

I am loading a website using a WebBrowser\'s Navigate function, and I want the browser to load the page with a cookie I\'ve given it.

The following code doesn\'t wor

2条回答
  •  时光说笑
    2020-11-30 07:10

    The proper way is using InternetSetCookieEx.

    [DllImport("wininet.dll")]
    static extern InternetCookieState InternetSetCookieEx(
        string lpszURL,
        string lpszCookieName,
        string lpszCookieData,
        int dwFlags,
        int dwReserved);
    
    enum InternetCookieState : int
    {
        COOKIE_STATE_UNKNOWN = 0x0,
        COOKIE_STATE_ACCEPT = 0x1,
        COOKIE_STATE_PROMPT = 0x2,
        COOKIE_STATE_LEASH = 0x3,
        COOKIE_STATE_DOWNGRADE = 0x4,
        COOKIE_STATE_REJECT = 0x5,
        COOKIE_STATE_MAX = COOKIE_STATE_REJECT
    } 
    

    Here's some code to test it at a website that shows your HTTP headers.

    InternetSetCookieEx("http://request.urih.com/", null, "TestData=Test;", 0, 0);
    webBrowser1.Navigate("http://request.urih.com/");
    

提交回复
热议问题