使用WebBrowser获取网页源代码

using System;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        bool completed = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string[] urls = new string[] { "http://www.mzwu.com/", "http://www.taobao.com/" };
            for (int i = 0; i < urls.Length; i++)
            {
                webBrowser1.Navigate(urls[i]);
                //同步获取解决方案
                completed = false;
                while (!completed)
                {
                    Application.DoEvents();
                }
            }
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser wb = sender as WebBrowser;
            //WebBrowserReadyState.Interactive和iframe会多次触发DocumentCompleted
            if (wb.ReadyState == WebBrowserReadyState.Complete && wb.Url.ToString() == e.Url.ToString())
            {
                //WebBrowser.DocumentText有可能乱码
                using (StreamReader stream = new StreamReader(wb.DocumentStream, Encoding.GetEncoding(wb.Document.Encoding)))
                {
                    string content = stream.ReadToEnd();
                    MessageBox.Show(wb.Url.ToString() + " length:" + content.Length);
                }
                completed = true;
            }
        }
    }
}

参考资料

[1].WebBrowser获取HTML问题:http://blog.csdn.net/zhuzhu837_1/article/details/7794651
[2].谨慎注意WebBrowser控件的DocumentCompleted事件:http://www.csharpwin.com/dotnetspace/1185.shtml

上一篇: 什么是API
下一篇: WebBrowser中动态给页面添加js
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
最新日志:
评论: 0 | 引用: 0 | 查看次数: 9322
发表评论
登录后再发表评论!