C#,WebBrowser,OnNavigate,触发,示例,代码
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Resources;
using System.IO;
using System.Text.RegularExpressions;
using System.Web;
using System.Runtime.InteropServices;
//using System.Runtime.InteropServices.ComTypes;
using AxSHDocVw;
using SHDocVw;
using System.Runtime.InteropServices.ComTypes;
namespace Flash采集
{
public partial class Form1 : Form,DWebBrowserEvents
{
//private UCOMIConnectionPoint icp;
private IConnectionPoint icp;
private int cookie = -1;
public Form1()
{
InitializeComponent();
IConnectionPointContainer icpc = (IConnectionPointContainer)ie.GetOcx(); // ADDed
Guid g = typeof(DWebBrowserEvents).GUID;
icpc.FindConnectionPoint(ref g, out icp);
//icpc.FindConnectionPoint(ref g, out icp);
icp.Advise(this, out cookie);
}
private void button1_Click(object sender, EventArgs e)
{
//获取目标网页代码
button1.Enabled = false;
Regex r;
MatchCollection mc;
string code = GetHttpPage(url.Text);
//匹配名字
r = new Regex(”<meta name=\”title\” content=\”(?<title>.*?)\”>[\\s\\S]*<meta name=\”keywords\” content=\”(?<keyword>.*?)\”>[\\s\\S]*<meta name=\”description\” content=\”(?<content>.*?)\”>[\\s\\S]*videoId2= ‘(?<id>.*?)’;[\\s\\S]*class=’userName’>(?<author>.*?)</a>”, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline);
mc = r.Matches(code);
if (mc.Count > 0)
{
t1.Text = mc[0].Result(”${title}”);
t2.Text = mc[0].Result(”${keyword}”);
t4.Text = mc[0].Result(”${content}”);
t5.Text = mc[0].Result(”${author}”);
t7.Text = “http://player.youku.com/player.php/sid/” + mc[0].Result(”${id}”) + “/v.swf”;
}
button1.Enabled = true;
//OK
}
#region 获取网页源码
private string GetHttpPage(string url)
{
string str = “”;
try
{
//Thread.Sleep(500);
Uri uri = new Uri(url);
HttpWebRequest hwReq = (HttpWebRequest)WebRequest.Create(uri);
hwReq.Method = “get”;
//hwReq.Timeout = 10;
HttpWebResponse hwRes = (HttpWebResponse)hwReq.GetResponse();
StreamReader reader = new StreamReader(hwRes.GetResponseStream(), System.Text.Encoding.GetEncoding(”utf-8″));
str = reader.ReadToEnd();
hwRes.Close();
reader.Dispose();
}
catch { str = “”; }
return str;
}
#endregion
private void tt_MouseClick(object sender, MouseEventArgs e)
{
((TextBox)sender).SelectAll();
}
private void Form1_Load(object sender, EventArgs e)
{
object obj = null;
object headers = “Name”;
ie.Navigate(”http://www.ts815.cn/index.asp“, ref obj, ref obj, ref obj, ref headers);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
public void BeforeNavigate(string URL, int Flags, string TargetFrameName,ref object PostData, string Headers, ref bool Cancel)
{
MessageBox.Show(URL + “\n” + PostData + “\n” + Headers, “DWebBrowser Event”);
}
public void PropertyChange(string Property) { }
public void NavigateComplete(string URL) { }
public void WindowActivate() { }
public void FrameBeforeNavigate(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Cancel) { }
public void NewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed) { }
public void FrameNewWindow(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed) { }
public void TitleChange(string Text) { }
public void DownloadBegin() { }
public void DownloadComplete() { }
public void WindowMove() { }
public void WindowResize() { }
public void Quit(ref bool Cancel) { }
public void ProgressChange(int Progress, int ProgressMax) { }
public void StatusTextChange(string Text) { }
public void CommandStateChange(int Command, bool Enable) { }
public void FrameNavigateComplete(string URL) { }
//////////////////////////////////////////////////////////////////////////////////////////////////////
}
}



