VBS xmlhttp函数例子

炒冷饭,xmlhttp的几个简单函数

Function download(url)  
    Dim xmlHttp
    Set xmlHttp = CreateObject("Microsoft.XMLHTTP")
    url = urlCheck(url)
    xmlHttp.open "get", url, False
    xmlHttp.send
    Do
    Loop Until xmlHttp.readyState = 4
    download = xmlHttp.responseText
    Set xmlHttp = Nothing
End Function

Sub downloadFile(url, filename)  
    Dim xmlHttp, aso
    Set xmlHttp = CreateObject("Microsoft.XMLHTTP")
    url = urlCheck(url)
    xmlHttp.open "get", url, False
    xmlHttp.send
    Do
    Loop Until xmlHttp.readyState = 4
    Set aso = CreateObject("Adodb.Stream")
    aso.Type = 1
    aso.mode = 3
    aso.open
    aso.write (xmlHttp.responseBody)
    aso.SaveToFile filename, 2
    aso.Close
    Set aso = Nothing
    Set xmlHttp = Nothing
End Sub

Function getHttp(url)  
    getHttp = download(url)
End Function

Function postHttp(url, parameter)  
    'parameter:p1=p1&p2=p2&p3=p3
    Dim xmlHttp, aso
    Set xmlHttp = CreateObject("Microsoft.XMLHTTP")
    url = urlCheck(url)
    xmlHttp.open "post", url, False
    xmlHttp.setrequestheader "content-type", "application/x-www-form-urlencoded"
    xmlHttp.send parameter
    Do
    Loop Until xmlHttp.readyState = 4
    postHttp = xmlHttp.responseText
    Set xmlHttp = Nothing
End Function

Function urlCheck(url)  
    If Left(url, 7) <> "http://" Then
        url = "http://" & url
    End If
    urlCheck = url
End Function