Internet技巧两则 一、判断某一个连接是否保存在Cache中 在使用Microsoft IE在网上冲浪时,IE会把你浏览过的网页保存在Cache中以便你可以脱机浏览。 下面这个程序可以判断一个URL是否在浏览器的Cache中。 首先建立一个新的VB工程文件,在Form1中加入一个CommandButton控件和一个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 ´获取错误消息 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没有在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 没有保存在Cache中.", vbInformation End If Exit Sub ErrorHandler: MsgBox "URL 没有保存在Cache中 [" & Err.Description & "]", vbInformation End Sub Private Sub Form_Load() Form1.CurrentX = 150: Form1.CurrentY = 60 Form1.Print "在Text1中输入URL,按Command1检测" Text1.Text = "" Command1.Default = True End Sub 运行程序,在TextBox中输入URL地址(例如http://member.netease.com/~blackcat),然后点击Command1按钮,如果URL在Cache中,程序会弹出消息框显示URL 保存在Cache中。
|