05-15-2012، 02:01 PM
Read an internet web page with API calls
کد:
Read an internet web page with API calls
The following routine uses API calls to read/download an internet file. A routine demonstrating how to use this code can be found at the bottom of this post.
Option Explicit
Private Declare Function InternetOpen Lib "wininet" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet" (ByVal hInet As Long) As Integer
Private Declare Function InternetReadFile Lib "wininet" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
Private Declare Function InternetOpenUrl Lib "wininet" Alias "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal lpszUrl As String, ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
'Purpose : Retreview text from a web site
'Inputs : sURL The URL and file name to extract the text from
' [lBufferSize] The number of characters to extract.
' If value is -1 the reads the whole page.
'Outputs : The text found on the web site
'Notes : NOT SUITABLE FOR ACCESSING THE INTERNET THROUGH A ....... SERVER
Function InternetGetText(sURL As String, Optional lBufferSize As Long = -1) As String
Dim lhOpen As Long, lhFile As Long, sBuffer As String, lRet As Long
Const clBufferIncrement As Long = 2000
Const scUserAgent = "VBUsers"
Const INTERNET_OPEN_TYPE_PRECONFIG = 0, INTERNET_FLAG_EXISTING_CONNECT = &H20000000
Const INTERNET_OPEN_TYPE_DIRECT = 1, INTERNET_OPEN_TYPE_PROXY = 3
Const INTERNET_FLAG_RELOAD = &H80000000
If lBufferSize = -1 Then
'Create an arbitary buffer to read the whole file in parts
sBuffer = String$(clBufferIncrement, Chr$(0))
Else
'Create a specified buffer size
sBuffer = String$(lBufferSize, Chr$(0))
End If
'Create an internet connection
lhOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_DIRECT, vbNullString, vbNullString, 0)
'Open the url
lhFile = InternetOpenUrl(lhOpen, sURL, vbNullString, ByVal 0&, INTERNET_FLAG_RELOAD, ByVal 0&)
If lhFile = 0 Then
'Try using p
InternetCloseHandle lhFile
InternetCloseHandle lhOpen
'Create an internet connection
lhOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
'Open the url
lhFile = InternetOpenUrl(lhOpen, sURL, vbNullString, ByVal 0&, INTERNET_FLAG_EXISTING_CONNECT, ByVal 0&)
End If
If lBufferSize = -1 And lhFile <> 0 Then
'Read the whole page
Do
InternetReadFile lhFile, sBuffer, clBufferIncrement, lRet
InternetGetText = InternetGetText & Left$(sBuffer, lRet)
Loop While lRet = clBufferIncrement
Else
'Read the specified number of bytes from the file
InternetReadFile lhFile, sBuffer, lBufferSize, lRet
InternetGetText = InternetGetText & Left$(sBuffer, lRet)
End If
'clean up
InternetCloseHandle lhFile
InternetCloseHandle lhOpen
End Function
'Demonstration routine
'(Note the Debug window will only show the last 255 lines)
Sub Test()
Dim sInterPage As String
sInterPage = InternetGetText("http://example")
Debug.Print sInterPage
End Sub