Why doesn't this code using InternetSetCookie to set cookies in a WebBroser control work?

I made this example to try to understand why I don’t send cookies at all using my WebBrowser, it’s pretty simple, it has a WebBrowser, that’s all:

namespace BrowserTest
{
    public partial class Form1 : Form
    {
        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool InternetSetCookie(string url, string name, string data);

        public static bool SetWinINETCookieString(string url, string name, string data)
        {
            return Form1.InternetSetCookie(url, name, data);
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // None of two works
            //SetWinINETCookieString("www.nonexistent.com", null, "dataToTest=thisIsTheData");
            SetWinINETCookieString("www.nonexistent.com", "dataToTest", "thisIsTheData");
            this.webBrowser1.Navigate("www.nonexistent.com");
        }
    }
}

And what Fidler says I am sending:

enter image description here

It seems that everyone who uses this feature is doing well, but for me life is that I can't get it to work. I tried on different computers and it also fails. Any help would be great, thanks.

+5
source share
2 answers

, . www.nonexistent.com URI, http://www.nonexistent.com

+1

. , InternetSetCookie, false, GetLastError, 87 - .

.

[DllImport("kernel32.dll")]
public static extern uint GetLastError();

......

bool ok = SetWinINETCookieString("www.nonexistent.com", "dataToTest", "thisIsTheData");
if (!ok)
{
  int errorCode = GetLastError(); //this will return 87  for www.nonexistent.com
}
+4

All Articles