Thứ Tư, 18 tháng 11, 2015

ASP, WebServiceClass

<%
'Thaihn
'Example
'set ws = new WebServiceClass
'Response.Write ws.SetUrl("http://domain.com/Service.asmx").SetMethod("name").AddParam("email", "asd@yahoo.com").AddParam("type", 2).Post().GetResponseText()

' Soap 1.1
' set ws = new WebServiceClass
' with ws
    ' .SetUrl("http://domain.com/Service.asmx")
    ' .SetSoapUrl("http://tempuri.org/name/Service1")
    ' .SetMethod("getObjectInfo")
    ' .AddParam "strContract", "asdasds"
    ' .Send("SOAP")
' end with
' Response.Write ws.GetResponseText

class WebServiceClass
    public pUrl
    public pSoapUrl
    public pMethod
    public pResponseText
    public pResponseXml
    public pParameters
    private xmlhttp



    public function Send(method)
        method = UCase(method)
     
        select case method
            case "POST":     set Send = ExecutePost()
            case "GET":     set Send = ExecuteGet()
            case "SOAP":     set Send = ExecuteSoap()
            case "SOAP 1.2":set Send = ExecuteSoap12()
            case else:
                Response.Write "Khong ho tro phuong thuc """& method &""""
                Response.End
        end select
    end function
 
    public function Post()
        set Post = ExecutePost()
    end function
 
    public function ExecutePost()
        ' Response.Write pUrl & "/" & pMethod
        ' Response.Write ParametersToString()
        ' Response.End
        if xmlHttp is nothing then Set xmlHttp = Server.CreateObject("MSXML2.ServerXMLHTTP")    'create object
        xmlhttp.open "POST", pUrl & "/" & pMethod, false
        xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        xmlhttp.send ParametersToString()
        pResponseText = xmlhttp.responseText
        set pResponseXml = xmlhttp.responseXML
        set ExecutePost = me
    end function
 
    public function ExecuteGet()
        if xmlHttp is nothing then Set xmlHttp = Server.CreateObject("MSXML2.ServerXMLHTTP")    'create object
        xmlhttp.open "GET", pUrl & "/" & pMethod & "?" & ParametersToString(), false
        xmlhttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
        xmlhttp.send()
        pResponseText = xmlhttp.responseText
        set ExecuteGet = me
    end function
 
    public function ExecuteSoap()
        if xmlHttp is nothing then Set xmlHttp = Server.CreateObject("MSXML2.ServerXMLHTTP")    'create object
     
        xmlhttp.open "POST", pUrl, false
        xmlhttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
        xmlhttp.setRequestHeader "SOAPAction", pSoapUrl & "/" & pMethod
        xmlhttp.send "<?xml version=""1.0"" encoding=""utf-8""?>"&_
            "<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">"&_
              "<soap:Body>"&_
                "<"& pMethod &" xmlns="""& pSoapUrl &""">"&_
                  ParametersToSoapString()&_
                "</"& pMethod &">"&_
              "</soap:Body>"&_
            "</soap:Envelope>"
     
        pResponseText = xmlhttp.responseText
        set ExecuteSoap = me
    end function
 
    public function ExecuteSoap12()
        if xmlHttp is nothing then Set xmlHttp = Server.CreateObject("MSXML2.ServerXMLHTTP")    'create object
     
        xmlhttp.open "POST", pUrl, false
        xmlhttp.setRequestHeader "Content-Type", "application/soap+xml; charset=utf-8"
        xmlhttp.send "<?xml version=""1.0"" encoding=""utf-8""?>"&_
            "<soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">"&_
              "<soap12:Body>"&_
                "<"& pMethod &" xmlns="""& pSoapUrl &""">"&_
                  ParametersToSoapString()&_
                "</"& pMethod &">"&_
              "</soap12:Body>"&_
            "</soap12:Envelope>"
     
        pResponseText = xmlhttp.responseText
        set ExecuteSoap12 = me
    end function
 
    public function SetUrl(url)
        pUrl = url
        set SetUrl = me
    end function
 
    public function SetSoapUrl(url)
        pSoapUrl = url
        set SetSoapUrl = me
    end function
 
    public function SetMethod(method)
        pMethod = method
        set SetMethod = me
    end function
 
    public function AddParam(pKey, pValue)
        pParameters(pKey) = pValue
        set AddParam = me
    end function
 
    public function ParametersToString()
        dim value
     
        for each key in pParameters.keys
            if value = "" then
                value = key & "=" & pParameters(key)
            else
                value = value & "&" & key & "=" & pParameters(key)
            end if
        next
     
        ParametersToString = value
    end function
 
    public function ParametersToSoapString()
        dim value
     
        for each key in pParameters.keys
            value = value & "<"& key &">"& pParameters(key) &"</"& key &">"
        next
     
        ParametersToSoapString = value
    end function
 
    public function GetResponseText()
        GetResponseText = pResponseText
    end function
 
    public function GetResultText()
        dim xml
     
        Set xml = Server.CreateObject("Microsoft.XMLDOM")
        if Err.Number <> 0 then
            GetResultText = "99|Can not create xml."
            set xml = nothing
            On Error Goto 0
            exit function
        end if
     
        xml.async = False
        xml.load (pResponseXml)
        if xml.parseError.errorcode <> 0 then
            GetResultText = "99|Load xml error: "&pResponseText
            set xml = nothing
            On Error Goto 0
            exit function
        else
            GetResultText = xml.documentElement.childNodes(0).text
        end if
    end function
 
    public function ClearParams()
        Set pParameters = CreateObject("Scripting.Dictionary")
    end function
 
 
    Private Sub Class_Initialize()
        Set pParameters = CreateObject("Scripting.Dictionary")
        set xmlHttp = nothing
    End Sub
 
    Private Sub Class_Terminate()
        Set pParameters = Nothing
        set xmlhttp = nothing
    End Sub
End class
%>

Không có nhận xét nào:

Đăng nhận xét