一、首先建立正则表达式
''' <summary>
''' 正则表达式(ByVal 被检字符, 检索规则 strng)
''' </summary>
''' <param name="patrn">被检字符</param>
''' <param name="strng">检索规则</param>
''' <returns>检索到的位置</returns>
''' <remarks></remarks>
Public Function RegExp(ByVal patrn, ByVal strng)
Dim regEx, Match, Matches, RetStr ' 建立变量。
RetStr = Nothing
regEx = New VBScript_RegExp_55.RegExp ' 建立正则表达式。
regEx.Pattern = patrn ' 设置模式。
regEx.IgnoreCase = True ' 设置是否区分大小写。
regEx.Global = True ' 设置全局替换。
Matches = regEx.Execute(strng) ' 执行搜索。
For Each Match In Matches ' 遍历 Matches 集合。
RetStr = Match.Value
Next
Return RetStr
End Function
二、建立一个获取网页源码的函数
''' <summary>
''' 获得网页信息(网址 As String) As String
''' </summary>
''' <param name="strURL">网址</param>
''' <returns>获得网页信息</returns>
''' <remarks></remarks>
Public Function GetWebText(strURL As String) As String
On Error Resume Next
Dim url As String = strURL
Dim httpReq As System.Net.HttpWebRequest
httpReq.ServicePoint.ConnectionLimit = 255
Dim httpResp As System.Net.HttpWebResponse
Dim httpURL As New System.Uri(url)
httpReq = CType(Net.HttpWebRequest.Create(httpURL), Net.HttpWebRequest)
httpReq.Method = "GET"
httpResp = CType(httpReq.GetResponse(), Net.HttpWebResponse)
httpReq.KeepAlive = False ' 获取或设置一个值,该值指示是否与Internet资源建立持久连接。
Dim reader As IO.StreamReader = New IO.StreamReader(httpResp.GetResponseStream, System.Text.Encoding.GetEncoding("GB2312")) '用GB2312字符
Dim respHTML As String = reader.ReadToEnd() 'respHTML就是网页源代码
Dim myip As String = ""
Dim myPort = ""
httpResp.Close()
If respHTML <> "" Then
url = ""
myip = ""
myPort = ""
End If
Return respHTML
End Function
三、建立获取网络时间的函数
'获得网络时间
''' <summary>
''' 网络时间()
''' </summary>
''' <param name="Style">定义时间格式</param>
''' <returns>网络时间</returns>
''' <remarks></remarks>
Function GetWebTime(Optional Style As Microsoft.VisualBasic.DateFormat = Nothing) As String
Dim WebText = GetWebText("http://open.baidu.com/special/time/")'从百度的北京时间获取页面源码
Dim regexp As New RegularExpressions
Dim WebTimes As String = RegExp("[0-9]{13}", WebText)'获取源码中时间信息
Dim WebTime As Date = #1/1/1970 8:00:00 AM#'定义初始时间
WebTime = DateAdd(DateInterval.Second, WebTimes / 1000, WebTime)'换算成现在的时间
If Style = Nothing Then'根据时间格式参数返回不同格式的时间信息
Return WebTime
Else
Return Format(WebTime, Style)
End If
End Function