c#WebBrowser进阶

WebBrowser的基本功能就是访问网页,但是由于它本身就不在主线程上面,所以程序判断它什么时候加载完成了,比较麻烦。为此我集合从网上找到的内容,做了一个例子。

其中包括了给WebBrowser设置cookies,点击WebBrowser中的button等功能。

  [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]

  public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData)

  private delegate void invokeDelegate();

  public void bk()

       {

                     

            string url = "http://*******"//任意网址

            //添加cookies

            for (int i = 0; i < cookies.Count; i++)

            {

                Cookie c = cookies[i];

                InternetSetCookie(url, c.Name, c.Value);

            }

            webBrowser2.Navigate(url);



            webBrowser2.ScriptErrorsSuppressed = true;

            while (true)

            {

                Application.DoEvents();

                if (webBrowser2.ReadyState != WebBrowserReadyState.Complete)

                {

                    break;

                }

            }



        

            invokeDelegate del = new invokeDelegate(StartMethod);



            IAsyncResult ir = del.BeginInvoke(AddComplete, null);



        }    

  /// <summary>

  /// 委托异步

  /// </summary>

  /// <param name="result"></param>

  private void AddComplete(IAsyncResult result)

    {

        invokeDelegate handler = (invokeDelegate)((AsyncResult)result).AsyncDelegate;

        handler.EndInvoke(result);

    }

  

 上面的代码是在webBrowser2中加载网页,使用了委托异步的方法。

 private void StartMethod()

   {

           

            Thread.Sleep(7000);

        

            this.Invoke(new Action(() =>

            {

                HtmlDocument cd = webBrowser2.Document;

                string doc = webBrowser2.DocumentText;           

                int count = 0;

                HtmlElementCollection dhl1 = cd.GetElementsByTagName("input");//GetElementsByTagName得到的是点击的标记元素名称



                foreach (HtmlElement item in dhl1)

                {

                    string elemName = item.GetAttribute("name");



                    if (elemName != null || elemName.Length > 0)

                    {

                        if (elemName == "btnclick")

                        {

                            item.InvokeMember("click");

                        }                    



                    }

                }



            }));

     

         

        }

  开始加入Thread.Sleep(7000);是为了给webBrowser2加载页面的时间,7s足够页面加载完成。然后在获取页面的内容,并找到btnclick进行点击。

最后贴出从txt中获取cookies的方法:

 public static CookieCollection getCookies(string path)

        {

            string cook = ReadTxt(path).Trim();



            CookieCollection cookies = new CookieCollection();

            string[] cookstr = cook.Split(';');

            foreach (string str in cookstr)

            {

                string[] cookieNameValue = str.Split('=');

                cookieNameValue[1] = cookieNameValue[1].Trim().ToString().Replace(",", "%2C");

                Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());

                ck.Domain = "*******";//Host地址,可从浏览器上查看

                cookies.Add(ck);



            }

            return cookies;

        }

  这样就可以实现自动加载某个网页并点击页面上的按钮了。

 

你可能感兴趣的:(WebBrowser)