窗体设计界面:
“转到”按钮的代码:
Private Sub btnGoto_Click(sender As Object,e As EventArgs) Handles btnGoto.Click wbMain.Navigate(txtUrl.Text) End Sub
获得网页基本信息:
Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click Dim doc As mshtml.IHTMLDocument doc = wbMain.Document.DomDocument txtInfo.Text = "协议:" & doc.protocol & vbCrLf txtInfo.Text &= "状态:" & doc.readyState & vbCrLf txtInfo.Text &= "编码:" & doc.charset & vbCrLf txtInfo.Text &= "Cookie:" & doc.cookie & vbCrLf txtInfo.Text &= "创建日期:" & doc.fileCreatedDate & vbCrLf txtInfo.Text &= "修改日期:" & doc.fileModifiedDate & vbCrLf txtInfo.Text &= "文件大小:" & doc.fileSize & vbCrLf txtInfo.Text &= "更新日期:" & doc.fileUpdatedDate & vbCrLf txtInfo.Text &= "最后修改日期:" & doc.lastModified & vbCrLf txtInfo.Text &= "Mime类型:" & doc.mimeType & vbCrLf txtInfo.Text &= "文档标题:" & doc.nameProp & vbCrLf txtInfo.Text &= "跳转来源:" & doc.referrer & vbCrLf txtInfo.Text &= "安全证书:" & doc.security & vbCrLf txtInfo.Text &= "文档标题:" & doc.title & vbCrLf txtInfo.Text &= "链接地址:" & doc.url & vbCrLf End Sub
除了IHTMLDocument接口,似乎直接用IHTMLDocument2、IHTMLDocument3这些都可以。
而且MSHtml还提供了HTMLDocument这个对象,也可以直接使用:
Private Sub Button2_Click(sender As Object,e As EventArgs) Handles Button2.Click Dim doc As mshtml.HTMLDocument doc = wbMain.Document.DomDocument txtInfo.Text = "协议:" & doc.protocol & vbCrLf txtInfo.Text &= "状态:" & doc.readyState & vbCrLf txtInfo.Text &= "编码:" & doc.charset & vbCrLf txtInfo.Text &= "Cookie:" & doc.cookie & vbCrLf txtInfo.Text &= "创建日期:" & doc.fileCreatedDate & vbCrLf txtInfo.Text &= "修改日期:" & doc.fileModifiedDate & vbCrLf txtInfo.Text &= "文件大小:" & doc.fileSize & vbCrLf txtInfo.Text &= "更新日期:" & doc.fileUpdatedDate & vbCrLf txtInfo.Text &= "最后修改日期:" & doc.lastModified & vbCrLf txtInfo.Text &= "Mime类型:" & doc.mimeType & vbCrLf txtInfo.Text &= "文档标题:" & doc.nameProp & vbCrLf txtInfo.Text &= "跳转来源:" & doc.referrer & vbCrLf txtInfo.Text &= "安全证书:" & doc.security & vbCrLf txtInfo.Text &= "文档标题:" & doc.title & vbCrLf txtInfo.Text &= "链接地址:" & doc.url & vbCrLf End Sub
获得的结果都一样:
需要注意的是,有时候获得fileCreatedDate、fileModifiedDate等可能出错,应该添加try catch语句。
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看 vb.net 教程 目录