具体的我也搞不大清楚,应该是发送一个请求吧(包括"Get"、"Post"、"Head")
我是用来返回待访问的资源是否存在,详细代码如下,郁闷的是执行到HttpSendRequest经常挂起:
Option Explicit
Public HttpQuery As HTTP_QUERY
Type HTTP_QUERY
STATUS_CODE As String
STATUS_TEXT As String
RAW_HEADERS_CRLF As String
End Type
Public Function OpenInternet(ByVal Url As String, Optional ByVal Path As String)
Dim hInternetOpen, hInternetConnect, hHttpOpenRequest, bRet
Dim sBuff As String * 1024
Dim lBuffLen As Long
Dim Temp_HttpQuery As HTTP_QUERY
If Not RunStop Then Exit Function
HttpQuery = Temp_HttpQuery
Url = Replace(Url, "http://", "")
If Url = "" Then Exit Function
hInternetOpen = InternetOpen("Open URL Application", _
INTERNET_OPEN_TYPE_PRECONFIG, _
vbNullString, _
vbNullString, _
0)
DoEvents
If (hInternetOpen <> 0) Then
' Type of service to access
hInternetConnect = InternetConnect(hInternetOpen, _
Url, _
INTERNET_DEFAULT_HTTP_PORT, _
vbNullString, _
"HTTP/1.0", _
INTERNET_SERVICE_HTTP, _
0, 0)
DoEvents
If hInternetConnect <> 0 Then
' Brings the data across the wire even if it locally cached
hHttpOpenRequest = HttpOpenRequest(hInternetConnect, _
"HEAD", _
Path, _
vbNullString, _
vbNullString, _
0, _
INTERNET_FLAG_RELOAD, 0)
DoEvents
If hHttpOpenRequest <> 0 Then
bRet = HttpSendRequest(hHttpOpenRequest, vbNullString, 0, vbNullString, 0)
If bRet Then
'response headers
lBuffLen = Len(sBuff)
If HttpQueryInfo(hHttpOpenRequest, HTTP_QUERY_STATUS_CODE, sBuff, lBuffLen, 0) = 1 Then
Temp_HttpQuery.STATUS_CODE = Replace(sBuff, Chr(0), "")
End If
lBuffLen = Len(sBuff)
If HttpQueryInfo(hHttpOpenRequest, HTTP_QUERY_STATUS_TEXT, sBuff, lBuffLen, 0) = 1 Then
Temp_HttpQuery.STATUS_TEXT = Replace(sBuff, Chr(0), "")
End If
lBuffLen = Len(sBuff)
If HttpQueryInfo(hHttpOpenRequest, HTTP_QUERY_RAW_HEADERS_CRLF, sBuff, lBuffLen, 0) = 1 Then
Temp_HttpQuery.RAW_HEADERS_CRLF = Replace(sBuff, Chr(0), "")
End If
DoEvents
End If
End If
End If
End If
' Close all handles
If hInternetOpen <> 0 Then
bRet = InternetCloseHandle(hInternetOpen)
End If
If hInternetConnect <> 0 Then
bRet = InternetCloseHandle(hInternetConnect)
End If
If hHttpOpenRequest <> 0 Then
bRet = InternetCloseHandle(hHttpOpenRequest)
End If
HttpQuery = Temp_HttpQuery
Debug.Print Replace(HttpQuery.STATUS_CODE, Chr(0), "")
Debug.Print Replace(HttpQuery.STATUS_TEXT, Chr(0), "")
'Debug.Print Replace(HttpQuery.RAW_HEADERS_CRLF, Chr(0), "")
End Function