HttpClient.asp

出自ProgWiki
跳至導覽 跳至搜尋

Windows HTTP Services版本

Class HttpClient
 
    Public WinHttpRequest
    Public UserName
    Public Password
    Public LoginMode
    public RequestHeaders
 
    'HttpRequest SetCredentials flags.
    Public Const HTTPREQUEST_SETCREDENTIALS_FOR_SERVER = 0
    Public Const HTTPREQUEST_SETCREDENTIALS_FOR_PROXY = 1
 
   ' Setup Initialize event.
    Private Sub Class_Initialize
        Set WinHttpRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
        Set UserName = ""
        Set Password = ""
        Set LoginMode = HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
        Set RequestHeaders = CreateObject("Scripting.Dictionary")
    End Sub
 
    ' Setup Terminate event.
    Private Sub Class_Terminate   
        Set WinHttpRequest = nothing
        Set RequestHeaders = nothing
    End Sub
 
    Public Function HttpGet(strUrl)
        WinHttpRequest.Open "GET", strUrl, False
 
        If (Len(UserName)<>0 and Len(Password)<>0) Then
            WinHttpRequest.SetCredentials( UserName, Password, LoginMode);
        End If
 
        RequestHeadersKeys = RequestHeaders.Keys
        For i = 0 To RequestHeaders.Count -1 
            HeaderKey = RequestHeadersKeys(i)
            WinHttpRequest.SetRequestHeader(HeaderKey, RequestHeaders.Item(HeaderKey))
        Next
 
        WinHttpRequest.Send
 
        If (WinHttpRequest.Status = 200) Then
            HttpGet = True
        Else
            HttpGet = False
        End if
    End Function
 
    'strPostData = "VAR1=" + Server.URLEncode(VAR1) +"&VAR2=" + Server.URLEncode(VAR2)
    Public Function HttpPost(strUrl, strPostData)
        WinHttpRequest.Open "POST", strUrl, False
 
        If (Len(UserName)<>0 and Len(Password)<>0) Then
            WinHttpRequest.SetCredentials( UserName, Password, LoginMode);
        End If
 
        RequestHeadersKeys = RequestHeaders.Keys
        For i = 0 To RequestHeaders.Count -1 
            HeaderKey = RequestHeadersKeys(i)
            WinHttpRequest.SetRequestHeader(HeaderKey, RequestHeaders.Item(HeaderKey))
        Next
 
        WinHttpRequest.Send strPostData
 
        If (WinHttpRequest.Status = 200) Then
            HttpPost = True
        Else
            HttpPost = False
        End If
    End Function
 
    Public Function SetRequestHeader(strHeader, strValue)
        RequestHeaders.Add(strHeader, strValue)
    End Function
 
    Public Property Get Status
        Set Status = WinHttpRequest.Status
    End Property 
 
    Public Property Get StatusText
        Set StatusText = WinHttpRequest.StatusText
    End Property 
 
    Public Property Get ResponseHeaders
        Set ResponseHeaders = WinHttpRequest.GetAllResponseHeaders()
    End Property 
 
    Public Property Get ResponseHeader(strHeader)
        Set ResponseHeader = WinHttpRequest.GetResponseHeader(strHeader)
    End Property 
 
    'return data of binary
    Public Property Get ResponseBody
        Set ResponseBody = WinHttpRequest.ResponseBody
    End Property 
 
    'return data of string
    Public Property Get ResponseText
        Set ResponseText = WinHttpRequest.ResponseText
    End Property 
 
End Class

IXMLHTTPRequest版本

Class HttpClient
    Public XMLHTTPRequest
    Public UserName
    Public Password
    Public RequestHeaders
 
   ' Setup Initialize event.
    Private Sub Class_Initialize
        Set XMLHTTPRequest = CreateObject("Msxml2.XMLHTTP.3.0")
        Set UserName = ""
        Set Password = ""
        Set RequestHeaders = CreateObject("Scripting.Dictionary")
    End Sub
 
    ' Setup Terminate event.
    Private Sub Class_Terminate   
        Set XMLHTTPRequest = nothing
        Set RequestHeaders = nothing
    End Sub
 
    Public Function HttpGet(strUrl)
        If (Len(UserName)<>0 and Len(Password)<>0) Then
            XMLHTTPRequest.open "GET", strUrl, False, UserName, Password
        Else
            XMLHTTPRequest.open "GET", strUrl, False
        End If
 
        RequestHeadersKeys = RequestHeaders.Keys
        For i = 0 To RequestHeaders.Count -1 
            HeaderKey = RequestHeadersKeys(i)
            XMLHTTPRequest.setRequestHeader HeaderKey, RequestHeaders.Item(HeaderKey)
        Next
 
        XMLHTTPRequest.send
 
        if (XMLHTTPRequest.readyState = 4) Then
            If (XMLHTTPRequest.Status = 200) Then
                HttpGet = True
                Exit Function
            End if
        End if
        HttpGet = False
    End Function
 
    Public Function HttpPost(strUrl, strPostData)
        If (Len(UserName)<>0 and Len(Password)<>0) Then
            XMLHTTPRequest.open "POST", strUrl, False, UserName, Password
        Else
            XMLHTTPRequest.open "POST", strUrl, False
        End If
 
        RequestHeadersKeys = RequestHeaders.Keys
        For i = 0 To RequestHeaders.Count -1 
            HeaderKey = RequestHeadersKeys(i)
            XMLHTTPRequest.setRequestHeader HeaderKey, RequestHeaders.Item(HeaderKey)
        Next
 
        RequestHeadersKeys = RequestHeaders.Keys
        For i = 0 To RequestHeaders.Count -1 
            HeaderKey = RequestHeadersKeys(i)
            XMLHTTPRequest.setRequestHeader HeaderKey, RequestHeaders.Item(HeaderKey)
        Next
 
        XMLHTTPRequest.send strPostData
 
        if (XMLHTTPRequest.readyState = 4) Then
            If (XMLHTTPRequest.Status = 200) Then
                HttpPost = True
                Exit Function
            End if
        End if
        HttpPost = False
    End Function
 
    Public Function SetRequestHeader(strHeader, strValue)
        RequestHeaders.Add(strHeader, strValue)
    End Function
 
    Public Property Get Status
        Set Status = XMLHTTPRequest.status
    End Property 
 
    Public Property Get StatusText
        Set StatusText = XMLHTTPRequest.statusText
    End Property 
 
    Public Property Get ResponseHeaders
        Set ResponseHeaders = XMLHTTPRequest.getAllResponseHeaders()
    End Property 
 
    Public Property Get ResponseHeader(strHeader)
        Set ResponseHeader = XMLHTTPRequest.getResponseHeader(strHeader)
    End Property 
 
    'return data of binary
    Public Property Get ResponseBody
        Set ResponseBody = XMLHTTPRequest.responseBody
    End Property 
 
    'return data of string
    Public Property Get ResponseText
        Set ResponseText = XMLHTTPRequest.responseText
    End Property 
 
    'return data of XML
    Public Property Get ResponseXML
        Set ResponseXML = XMLHTTPRequest.responseXML
    End Property 
 
    'return data of IStream
    Public Property Get ResponseStream
        Set ResponseStream = XMLHTTPRequest.responseStream
    End Property 
End Class