Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
887 views
in Technique[技术] by (71.8m points)

c# - Setting a cookie in a WebBrowser control

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 work:

wb.Navigate(url, null, null, "Cookie: " + cookie + "
");

What am I doing wrong? Will I have to use InternetSetCookie? This doesn't seem like the best solution.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Looks like there is a better way:

Import the InternetSetCookie function:

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

Create the Cookie object:

Cookie temp1 = new Cookie("KEY1", "VALUE1", "/Path/To/My/App", "/");

Call InternetSetCookie function to set the cookie for that URL

InternetSetCookie("https://my.url.com/Path/To/My/App", null, temp1.ToString() + "; expires = Sun, 01-Jan-2013 00:00:00 GMT");

Navigate the WebBrowser to the URL you would like to go to.

webBrowser1.Navigate("https://my.url.com/Path/To/My/App");

Think this is the best solution for the issue :).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...