private WebBrowser wb;
  private bool _isLogin = false;
  private void button3_Click(object sender, EventArgs e)
        {
            wb = new WebBrowser();            wb.Location = new System.Drawing.Point(12, 68);
            wb.MinimumSize = new System.Drawing.Size(1, 1);
            wb.Name = "webBrowser1";
            wb.Size = new System.Drawing.Size(1, 1);
            wb.TabIndex = 15;
            wb.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);
                        this.Controls.Add(wb);            Thread t = new Thread(new ParameterizedThreadStart(this.Begin));
            t.IsBackground = true;
            t.SetApartmentState(ApartmentState.STA);
            t.Start(wb);
        } private void Begin(object param)
        {
            (param as WebBrowser).Navigate("http://www.163.com");            while (true)
            {
                if (_isLogin)
                {
                    break;
                }
            }            String aa = (param as WebBrowser).Document.Cookie;    
        }  private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (e.Url.Equals("http://www.163.com"))
            {
                _isLogin = true;
            }        }我点击btn3,开一条STA的线程,将WebBrowser作为参数传入方法Begin中去跳转到163.com ,判断是否加载是否完成后,想获取WebBrowser里面的Cookie,可是就在String aa = (param as WebBrowser).Document.Cookie;  这里就出错了,错误是:指定的转换无效~~~~请问有什么办法可以解决了???

解决方案 »

  1.   

    可是我已经设置了
    t.SetApartmentState(ApartmentState.STA);WebBrowser不是只能支持STA的线程??
      

  2.   

    你可以在主线程里面定义一个委托,比如,
            public delegate void TestConnectEventHandler(TestConnectEvent ev);       public static event TestConnectEventHandler TestHandler;
    然后在主线程中初始化TestHandler,
            TestHandler+=new TestConnectEventHandler(LoginForm_TestHandler);
    然后再在对应的回调函数private void LoginForm_TestHandler(TestConnectEvent ev)中写如下代码:
            object[] pList = new object[] { this, ev };
           if (((LoginForm)Control.FromHandle(Program.hLoginDialog))!=null)
              ((LoginForm)Control.FromHandle(Program.hLoginDialog)).BeginInvoke        (new LoginFormTestHandler(LoginTestProc), pList); 
    这里面的LoginFormTestHandler也是一个委托(public delegate void LoginFormTestHandler(object sender,TestConnectEvent ev);LoginTestProc是它对应的回调函数),在回调函数
     private void LoginTestProc(object sender,TestConnectEvent ev)中就可以使用主线程里面的控件了
      

  3.   

    比如,String Name=textbox1.Text,这类的操作,楼主的读取cookie的操作也一样
      

  4.   


    不太明白,可以帮忙写一下吗??就是跳转到163后,把它的cookie重webBrowser中获取出来
      

  5.   


     private WebBrowser wb;
     private bool _isLogin = false;
     private IntPtr main;
      private void button3_Click(object sender, EventArgs e)
            {
                main=this.Handle;            Navigated+=new NavigatedHandler(NavigatedProc);            wb = new WebBrowser();            wb.Location = new System.Drawing.Point(12, 68);
                wb.MinimumSize = new System.Drawing.Size(1, 1);
                wb.Name = "webBrowser1";
                wb.Size = new System.Drawing.Size(1, 1);
                wb.TabIndex = 15;
                wb.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);
                            this.Controls.Add(wb);            Thread t = new Thread(new ParameterizedThreadStart(this.Begin));
                t.IsBackground = true;
                t.SetApartmentState(ApartmentState.STA);
                t.Start(wb);
            }
      public delegate void NavigatedHandler(EventArgs ev); public static event NavigatedHandler Navigated; public void NavigatedProc(EventArgs ev)
    {
       object[] pList = new object[] { this, ev };
       if (((窗体类型)Control.FromHandle(main))!=null)
          ((窗体类型)Control.FromHandle(main)).BeginInvoke (new NavigatedFormHandler(NavigatedFormProc), pList);   } public delegate void NavigatedFormHandler(EventArgs ev);
     
     public void  NavigatedFormProc(object sender, EventArgs e)
    {
       String aa = (param as WebBrowser).Document.Cookie; 
     }  private void Begin(object param)
            {
                (param as WebBrowser).Navigate("http://www.163.com");            while (true)
                {
                    if (_isLogin)
                    {
                        break;
                    }
                }
                EventArgs e=new EventArgs();
                if(Navigated!=null)
                  Navigated(e);
                    
            }  private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                if (e.Url.Equals("http://www.163.com"))
                {
                    _isLogin = true;
                }        }
      

  6.   


    String aa = (param as WebBrowser).Document.Cookie; 在哪里来的?