Private Declare Function InternetOpenA Lib "wininet.dll" (ByVal lpszAgent As String, ByVal dwAccessType As Long, ByVal lpszProxyName As String, ByVal lpszProxyBypass As String, ByVal dwFlags As Long) As Long
Private Declare Function InternetOpenUrlA Lib "wininet.dll" (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
Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal lpBuffer As String, ByVal dwNumberOfBytesToRead As Long, ByRef lpNumberOfBytesRead As Long) As Integer
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInternet As Long) As Integer
Private Sub Form_Load()
Dim hInternetSession As Long
Dim hUrl As Long
Dim buffer As String * 4096
Dim bytesRead As Long
hInternetSession = InternetOpenA("VB6App", 0, vbNullString, vbNullString, 0)
If hInternetSession <> 0 Then
hUrl = InternetOpenUrlA(hInternetSession, "http://www.tugaga.com/tool", vbNullString, 0, 0, 0)
If hUrl <> 0 Then
' 读取数据
InternetReadFile hUrl, buffer, Len(buffer), bytesRead
' 在这里处理接收到的数据(响应)
MsgBox Left$(buffer, bytesRead)
' 关闭 URL
InternetCloseHandle hUrl
End If
' 关闭 WinINet
InternetCloseHandle hInternetSession
End If
End Sub
下面是封装好的API函数
Private Declare Function InternetOpenA Lib "wininet.dll" (ByVal lpszAgent As String, ByVal dwAccessType As Long, ByVal lpszProxyName As String, ByVal lpszProxyBypass As String, ByVal dwFlags As Long) As Long
Private Declare Function InternetOpenUrlA Lib "wininet.dll" (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
Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal lpBuffer As String, ByVal dwNumberOfBytesToRead As Long, ByRef lpNumberOfBytesRead As Long) As Integer
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInternet As Long) As Integer
Function fangwen(ByVal url As String) As String
Dim hInternetSession As Long
Dim hUrl As Long
Dim buffer As String * 4096
Dim bytesRead As Long
fangwen = ""
hInternetSession = InternetOpenA("VB6App", 0, vbNullString, vbNullString, 0)
If hInternetSession <> 0 Then
hUrl = InternetOpenUrlA(hInternetSession, url, vbNullString, 0, 0, 0)
If hUrl <> 0 Then
InternetReadFile hUrl, buffer, Len(buffer), bytesRead
fangwen = Left$(buffer, bytesRead)
InternetCloseHandle hUrl
End If
InternetCloseHandle hInternetSession
End If
End Function
调用 MsgBox fangwen("http://www.tugaga.com")