Internet技巧兩則
一、判斷某一個(gè)連接是否保存在Cache中
在使用Microsoft IE在網(wǎng)上沖浪時(shí),IE會(huì)把你瀏覽過(guò)的網(wǎng)頁(yè)保存在Cache中以便你可以脫機(jī)瀏覽。
下面這個(gè)程序可以判斷一個(gè)URL是否在瀏覽器的Cache中。
首先建立一個(gè)新的VB工程文件,在Form1中加入一個(gè)CommandButton控件和一個(gè)TextBox控件,然后
在Form1的代碼窗口中加入以下代碼:
Option Explicit
Private Const ERROR_INSUFFICIENT_BUFFER = 122
Private Const eeErrorBase = 26720
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type INTERNET_CACHE_ENTRY_INFO
dwStructSize As Long
lpszSourceUrlName As String
lpszLocalFileName As String
CacheEntryType As String
dwUseCount As Long
dwHitRate As Long
dwSizeLow As Long
dwSizeHigh As Long
LastModifiedTime As FILETIME
ExpireTIme As FILETIME
LastAccessTime As FILETIME
LastSyncTime As FILETIME
lpHeaderInfo As Long
dwHeaderInfoSize As Long
lpszFileExtension As String
dwReserved As Long
End Type
Private Declare Function GetUrlCacheEntryInfo Lib "wininet.dll" Alias _
"GetUrlCacheEntryInfoA" _
(ByVal sUrlName As String, _
lpCacheEntryInfo As Any, _
lpdwCacheEntryInfoBufferSize As Long _
) As Long
Private Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
Private Const FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000
Private Const FORMAT_MESSAGE_FROM_HMODULE = &H800
Private Const FORMAT_MESSAGE_FROM_STRING = &H400
Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Private Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200
Private Const FORMAT_MESSAGE_MAX_WIDTH_MASK = &HFF
Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" _
(ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, _
ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As _
Long, Arguments As Long) As Long
Public Function WinAPIError(ByVal lLastDLLError As Long) As String
Dim sBuff As String
Dim lCount As Long
´獲取錯(cuò)誤消息
sBuff = String$(256, 0)
lCount = FormatMessage( _
FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS, _
0, lLastDLLError, 0&, sBuff, Len(sBuff), ByVal 0)
If lCount Then
WinAPIError = Left$(sBuff, lCount)
End If
End Function
Public Function GetCacheEntryInfo(ByVal hWnd As Long, ByVal lpszUrl As String) As Boolean
Dim dwEntrySize As Long
Dim lpCacheEntry As INTERNET_CACHE_ENTRY_INFO
Dim dwTemp As Long
Dim lErr As Long
If (GetUrlCacheEntryInfo(lpszUrl, ByVal 0&, dwEntrySize)) = 0 Then
lErr = Err.LastDllError
If (lErr <> ERROR_INSUFFICIENT_BUFFER) Then
´URL沒(méi)有在Cache中
Err.Raise eeErrorBase + 1, App.EXEName & ".mCacheEntry", WinAPIError(lErr)
GetCacheEntryInfo = False
Exit Function
Else
´URL保存在Cache中
GetCacheEntryInfo = True
End If
End If
End Function
Private Sub Command1_Click()
On Error GoTo ErrorHandler
If (GetCacheEntryInfo(Me.hWnd, Text1.Text)) Then
MsgBox "URL 保存在Cache中.", vbInformation
Else
MsgBox "URL 沒(méi)有保存在Cache中.", vbInformation
End If
Exit Sub
ErrorHandler:
MsgBox "URL 沒(méi)有保存在Cache中 [" & Err.Description & "]", vbInformation
End Sub
Private Sub Form_Load()
Form1.CurrentX = 150: Form1.CurrentY = 60
Form1.Print "在Text1中輸入U(xiǎn)RL,按Command1檢測(cè)"
Text1.Text = ""
Command1.Default = True
End Sub
運(yùn)行程序,在TextBox中輸入U(xiǎn)RL地址(例如http://member.netease.com/~blackcat),然后點(diǎn)擊Command1按鈕,如果URL在Cache中,程序會(huì)彈出消息框顯示URL 保存在Cache中。
一、判斷某一個(gè)連接是否保存在Cache中
在使用Microsoft IE在網(wǎng)上沖浪時(shí),IE會(huì)把你瀏覽過(guò)的網(wǎng)頁(yè)保存在Cache中以便你可以脫機(jī)瀏覽。
下面這個(gè)程序可以判斷一個(gè)URL是否在瀏覽器的Cache中。
首先建立一個(gè)新的VB工程文件,在Form1中加入一個(gè)CommandButton控件和一個(gè)TextBox控件,然后
在Form1的代碼窗口中加入以下代碼:
Option Explicit
Private Const ERROR_INSUFFICIENT_BUFFER = 122
Private Const eeErrorBase = 26720
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type INTERNET_CACHE_ENTRY_INFO
dwStructSize As Long
lpszSourceUrlName As String
lpszLocalFileName As String
CacheEntryType As String
dwUseCount As Long
dwHitRate As Long
dwSizeLow As Long
dwSizeHigh As Long
LastModifiedTime As FILETIME
ExpireTIme As FILETIME
LastAccessTime As FILETIME
LastSyncTime As FILETIME
lpHeaderInfo As Long
dwHeaderInfoSize As Long
lpszFileExtension As String
dwReserved As Long
End Type
Private Declare Function GetUrlCacheEntryInfo Lib "wininet.dll" Alias _
"GetUrlCacheEntryInfoA" _
(ByVal sUrlName As String, _
lpCacheEntryInfo As Any, _
lpdwCacheEntryInfoBufferSize As Long _
) As Long
Private Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
Private Const FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000
Private Const FORMAT_MESSAGE_FROM_HMODULE = &H800
Private Const FORMAT_MESSAGE_FROM_STRING = &H400
Private Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Private Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200
Private Const FORMAT_MESSAGE_MAX_WIDTH_MASK = &HFF
Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" _
(ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, _
ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As _
Long, Arguments As Long) As Long
Public Function WinAPIError(ByVal lLastDLLError As Long) As String
Dim sBuff As String
Dim lCount As Long
´獲取錯(cuò)誤消息
sBuff = String$(256, 0)
lCount = FormatMessage( _
FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS, _
0, lLastDLLError, 0&, sBuff, Len(sBuff), ByVal 0)
If lCount Then
WinAPIError = Left$(sBuff, lCount)
End If
End Function
Public Function GetCacheEntryInfo(ByVal hWnd As Long, ByVal lpszUrl As String) As Boolean
Dim dwEntrySize As Long
Dim lpCacheEntry As INTERNET_CACHE_ENTRY_INFO
Dim dwTemp As Long
Dim lErr As Long
If (GetUrlCacheEntryInfo(lpszUrl, ByVal 0&, dwEntrySize)) = 0 Then
lErr = Err.LastDllError
If (lErr <> ERROR_INSUFFICIENT_BUFFER) Then
´URL沒(méi)有在Cache中
Err.Raise eeErrorBase + 1, App.EXEName & ".mCacheEntry", WinAPIError(lErr)
GetCacheEntryInfo = False
Exit Function
Else
´URL保存在Cache中
GetCacheEntryInfo = True
End If
End If
End Function
Private Sub Command1_Click()
On Error GoTo ErrorHandler
If (GetCacheEntryInfo(Me.hWnd, Text1.Text)) Then
MsgBox "URL 保存在Cache中.", vbInformation
Else
MsgBox "URL 沒(méi)有保存在Cache中.", vbInformation
End If
Exit Sub
ErrorHandler:
MsgBox "URL 沒(méi)有保存在Cache中 [" & Err.Description & "]", vbInformation
End Sub
Private Sub Form_Load()
Form1.CurrentX = 150: Form1.CurrentY = 60
Form1.Print "在Text1中輸入U(xiǎn)RL,按Command1檢測(cè)"
Text1.Text = ""
Command1.Default = True
End Sub
運(yùn)行程序,在TextBox中輸入U(xiǎn)RL地址(例如http://member.netease.com/~blackcat),然后點(diǎn)擊Command1按鈕,如果URL在Cache中,程序會(huì)彈出消息框顯示URL 保存在Cache中。