欢迎光临

木子屋

标题: 使用WebBrowser获取网页源代码

作者: dnawo

日期: 2008-11-22 12:03 AM

分类: Win编程

内容: 复制内容到剪贴板 程序代码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

+ 查看当前日志评论 (0)

登录

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 3.797 ms

返回

标题: 使用WebBrowser获取网页源代码

你没有权限发表评论

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 3.797 ms

返回

暂无评论

返回

────────────

木子屋

PJBlog3 v3.2.9.518 Inside.

Processed In 3.859 ms

返回