有关webbrowser使用cookie自动登录的问题
xieye 2010-10-14 02:53:06 我想做一个可以切换浏览多个论坛的工具,同时可以自动登录。
具体想法是使用tabcontrol,每个tab下面放一个webbrowser,在第一次登录后保存cookie到本地目录;在下次打开时直接找到这个目录里的cookie,自动登录该论坛。
登录后读取cookie并写入文本:
string cookieStr1 = webBrowser1.Document.Cookie;
StreamWriter swtest = new StreamWriter("test1.txt");
swtest.Write(cookieStr);
swtest.Close();
可以读取cookie并保存到当前test1.txt;但是如何在webbrowser打开时设置这个cookie呢?
找了一些资料,这样写了代码:
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);
StreamReader swtest = new StreamReader("test1.txt");
string cookieStr = swtest.ReadToEnd();
swtest.Close();
string[] cookstr = cookieStr.Split(';');
Cookie ck = new Cookie("TEST", cookieStr, null, url);
InternetSetCookie(url, "TEST", ck.Value);
webBrowser1.Navigate(url);
其中url是网址。
这样做的话总是没法加上cookie的,每次都是登录界面,请问应该怎么改?
谢谢!~