怎么能看网页使用WebBrowser控件 [英] How Can Read Web Page Using WebBrowser control

查看:152
本文介绍了怎么能看网页使用WebBrowser控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string urlt = webBrowser1.Url.ToString();
Webbrowser1.Navigate("Google.com")

        HtmlElement elem;
        if (webBrowser1.Document != null)
        {
            HtmlElementCollection elems = webBrowser1.Document.GetElementsByTagName("HTML");
            if (elems.Count == 1)
            {
                elem = elems[0];
                string pageSource = elem.InnerHtml;

                if (pageSource == "404" || pageSource == "Inter" || pageSource == "siteblocked")
                {


                }
                else
                {

                    Ret2.Add("Page.." + "Url..." + urlt);

                }

我使用上述code在DocumentCompleted事件看好康但如果我
使用For循环为一个以上的网址,却没有打电话来DocumentCompleted事件每次请建议如果有好主意。

Am Using above mentioned code for reading WebPage at "DocumentCompleted" Event But If I am Using "For loop" for more then one Url It not calling to DocumentCompleted Event everytime Please suggest if any good idea.

推荐答案

从评论:

..但异步或伺机不支持我想到用VS2010 IAM和我
  已安装的NuGet但仍IAM发现异步的关键字,请
  帮助

.. but async or await is not supported i think iam using vs2010 and i already installed Nuget but still iam finding async keyword, please help

如果您不能使用异步/的await ,那么你就不能使用异步循环 web浏览器导航,除非诉诸去precated与的DoEvents 。使用状态模式,这就是C#5.0编译器生成幕后为异步/的await

If you can't use async/await, then you can't use for loop for asynchronous WebBrowser navigation, unless resorting to deprecated hacks with DoEvents. Use the state pattern, that's what C# 5.0 compiler generates behind the scene for async/await.

另外,如果你有足够的冒险,你可以模拟异步/的await 收益,描述< A HREF =htt​​p://stackoverflow.com/a/22291625/1768303>这里。

Alternatively, if you're adventurous enough, you can simulate async/await with yield, as described here.

更新,下面是使用C#枚举状态机的另一种方式(用C#2.0和更高版本兼容):

Updated, below is another way of exploiting the C# enumerator state machine (compatible with C# 2.0 and later):

using System;
using System.Collections;
using System.Windows.Forms;

namespace WindowsForms_22296644
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        IEnumerable GetNavigator(string[] urls, MethodInvoker next)
        {
            WebBrowserDocumentCompletedEventHandler handler =
                delegate { next(); };

            this.webBrowser.DocumentCompleted += handler;
            try
            {
                foreach (var url in urls)
                {
                    this.webBrowser.Navigate(url);
                    yield return Type.Missing;
                    MessageBox.Show(this.webBrowser.Document.Body.OuterHtml);
                }
            }
            finally
            {
                this.webBrowser.DocumentCompleted -= handler;
            }
        }

        void StartNavigation(string[] urls)
        {
            IEnumerator enumerator = null;
            MethodInvoker next = delegate { enumerator.MoveNext(); };
            enumerator = GetNavigator(urls, next).GetEnumerator();
            next();
        }

        private void Form_Load(object sender, EventArgs e)
        {
            StartNavigation(new[] { 
                "http://example.com",
                "http://example.net",
                "http://example.org" });
        }
    }
}

这篇关于怎么能看网页使用WebBrowser控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆