Link to home
Start Free TrialLog in
Avatar of sumitarigapudi
sumitarigapudi

asked on

How to connect to a web service, and read the xml string response from webservice in asp

There is a webservice called somename.asmx. The service has 5 methods.
I need to send data to it and get the data from webservice.

I am trying to use the soap version 3.0 to connect to webservice.
and trying to use Msxml2.DOMDocument to read the data. i followed this linik
http://www.wimdows.net/articles/article.aspx?aid=11

I am sucessful in connecting to webservice. But soap toolkit is trial version, do i need to buy this.
is there any other way to connect to webservice.
and how to parse the xml string that i get from webservice.
Avatar of anderson22
anderson22

You will want to use the Microsoft Xml Parser.  First, download and install the latest version (4) on your server where you are running the asp.  To help in communicating with the webservice, you can use the following routine:

Example of use:
'To post the following SOAP request
'            POST /ASPPay/Service.asmx HTTP/1.1
'            Host: 207.13.7.199
'            Content-Type: text/xml; charset=utf-8
'            Content-Length: length
'            SOAPAction: "http://www.testasp.com/ASPPay/GetAllowedTransactions"
'
'            <?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>
'                <GetAllowedTransactions xmlns="http://www.testasp.com/ASPPay/">
'                  <strCustomerID>string</strCustomerID>
'                </GetAllowedTransactions>
'              </soap:Body>
'            </soap:Envelope>

'Use the following code
'            Set objSoapRequest = New SoapRequest
'            objSoapRequest.OpenHTTPConnection "POST", "The URL to the web service", "The web service namespace", "GetAllowedTransactions"
'            objSoapRequest.StartBody()
'            objSoapRequest.AddMember "strCustomerID", strCustomerID
'            objSoapRequest.EndBody()
'            objSoapRequest.SendRequest()
'
'            'Retreive the bSuccess variable
'            somevar = objSoapRequest.Extract("bSuccess")
'
'            'Retreive the bAllowACH variable
'            somevar = objSoapRequest.Extract("bAllowACH")
'
'            ...
'
'            Set objSoapRequest = nothing

The Object (put this code into it's own file, SoapTools.asp, and then include it in your page where you make the call to the webservice):

--- START ---
'SoapRequest - Provides ability to send/receive/parse soap messages
Class SoapRequest

      'Initialize Variables
      Private objXmlHTTP
      Private strSoapBody
      Private strNameSpace
      Private strEndPoint
      Private strMethod
      Private strURL
      Private strStructNameSpace
      Private bRequestSent
      Private bResponseReceived
      Private bInsideStructure
      Private strResponseText
      
      Private Sub Class_Initialize
            Set objXmlHTTP            = Server.CreateObject("Msxml2.XMLHTTP.4.0")
            strSoapBody                  = ""
            strNameSpace            = ""
            strEndPoint                  = ""
            strMethod                  = ""
            strURL                        = ""
            strStructNameSpace      = ""
            bRequestSent            = false
            bResponseReceived      = false
            bInsideStructure      = false
            strResponseText            = ""
      End Sub
      
      Public Sub OpenHTTPConnection(Method,URL,NameSpace,EndPoint)
            strMethod            = Method
            strURL                  = URL
            strNameSpace      = NameSpace
            strEndPoint            = EndPoint
      End Sub
      
      Private Sub AddHeader(Header, Value)
            objXmlHTTP.setRequestHeader Header, Value
      End Sub
      
      Public Sub StartBody()
            strSoapBody = "<?xml version=""1.0"" encoding=""utf-8""?>" & vbCrLf
            strSoapBody = strSoapBody & "<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/"">" & vbCrLf
            strSoapBody = strSoapBody & "<soap:Body>" & vbCrLf
            strSoapBody = strSoapBody & "<" & strEndPoint & " xmlns=""" & strNameSpace & """>" & vbCrLf
      End Sub
      
      Public Sub StartStruct(Name,StructNameSpace)
            strSoapBody                  = strSoapBody & "<" & Name & ">" & vbCrLf
            strStructNameSpace      = StructNameSpace
            bInsideStructure      = true
      End Sub
      
      Public Sub AddMember(Name,Value)
            if bInsideStructure and Len(strStructNameSpace) > 0 then
                  strSoapBody = strSoapBody & "<" & Name & " xmlns=""" & strStructNameSpace & """>" & Value & "</" & Name & ">" & vbCrLf
            else
                  strSoapBody = strSoapBody & "<" & Name & ">" & Value & "</" & Name & ">" & vbCrLf      
            end if
      End Sub
      
      Public Sub EndStruct(Name)
            strSoapBody                  = strSoapBody & "</" & Name & ">" & vbCrLf
            strStructNameSpace      = ""
            bInsideStructure      = false
      End Sub
      
      Public Sub EndBody()
            strSoapBody = strSoapBody & "</" & strEndPoint & ">" & vbCrLf
            strSoapBody = strSoapBody & "</soap:Body>" & vbCrLf
            strSoapBody = strSoapBody & "</soap:Envelope>"
      End Sub
      
      Public Sub SendRequest()
            if not bRequestSent then
                  objXmlHTTP.open strMethod, strURL, false

                  AddHeader "Content-Type", "text/xml"
                  if Right(strNameSpace,1) = "/" then
                        AddHeader "SOAPAction", strNameSpace & strEndPoint
                  else
                        AddHeader "SOAPAction", strNameSpace & "/" & strEndPoint
                  end if
            
                  objXmlHTTP.send strSoapBody
                  bRequestSent = true
                  
                  strResponseText = objXmlHTTP.responseText
                  bResponseReceived = true
            end if
      End Sub
      
      Public Function GetResults()
            if bRequestSent then
                  GetResults = strResponseText
            else
                  GetResults = ""
            end if
      End Function
      
      Public Function Extract(Name)
            Extract = ""
            if bResponseReceived then
                  if Instr(strResponseText,Name) > 0 then
                        Extract = Mid(strResponseText,( Instr( Instr(strResponseText,Name) , strResponseText, ">") + 1 ),( Instr( ( Instr( Instr(strResponseText,Name) , strResponseText, ">") + 1 ),strResponseText,"<") - ( Instr( Instr(strResponseText,Name) , strResponseText, ">") + 1 ) ) )
                        if Len(Extract) > 0 then
                              if Left(Name,1) = "b" then
                                    Extract = CBool(Extract)
                              elseif Left(Name,1) = "d" then
                                    Extract = CDbl(Extract)
                              elseif Left(Name,1) = "i" then
                                    Extract = CInt(Extract)
                              end if
                        end if
                  end if
            end if
      End Function
      
      Public Function GetSoapBody()
            GetSoapBody = strSoapBody
      End Function
      
      Private Sub Class_Terminate
            Set objXmlHTTP = nothing
      End Sub
      
End Class
--- STOP ---

Or, download the file here:
http://www.pebuk.com/resources/SoapTools.zip

-rca
Avatar of sumitarigapudi

ASKER

Hi anderson,

do i need soap toolkit version 4 or the
xml parser 4 version.

The links that you have given me do not work.

let me send you my code:
the method is TestResponse
i can connect to webservice and i am also getting data into str1.
but i need to parse xml data that i receive. i am not able to load str1 into the oDOM object.
then i have to get the node info.

right now i have trial version of soap tool kit  version3

<%
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then

Dim oSOAP
dim nodesText,nodesTransid, nodesResponse

    Set oSOAP = Server.CreateObject("MSSOAP.SoapClient30")
      Set oDOM = Server.CreateObject("Msxml2.DOMDocument.4.0")
'      oDOM.async = false

      
    oSOAP.ClientProperty("ServerHTTPRequest") = True
    oSOAP.mssoapinit("http://somename/Service1.asmx?wsdl")

      'oDOM.loadXML(oSOAP.TestResponse(Clng(Request.Form("text1")), Cstr(Request.Form("text2")),Cstr(Request.Form("text3")),Cstr(Request.Form("text4")),Cstr(Request.Form("text5")),Cstr(Request.Form("text6"))))

      dim str1
      str1 = oSOAP.TestResponse(Clng(Request.Form("text1")), Cstr(Request.Form("text2")),Cstr(Request.Form("text3")),Cstr(Request.Form("text4")),Cstr(Request.Form("text5")),Cstr(Request.Form("text6")))

      
      If Not oDOM.Load(str1) Then
            response.write "could not load the file"  & "<BR>"
            response.end
      end if
      set aNode = oDOM.documentElement.xmlResponse
      response.write ("the response is: " & aNode)

   
      Response.write("TestResponse " & oSOAP.TestResponse(Clng(Request.Form("text1")), Cstr(Request.Form("text2")),Cstr(Request.Form("text3")),Cstr(Request.Form("text4")),Cstr(Request.Form("text5")),Cstr(Request.Form("text6"))) & "<BR>")

      
      %>
xml parser 4, the soap toolkit is not necessary

-rca
where do i call the methods in the webserice and how to pass values into it.

name of method is testresponse
and  i need to send 6 values.
below is the sample request (requires the SoapTools.asp):


Set objSoapRequest = New SoapRequest
objSoapRequest.OpenHTTPConnection "POST", "("http://somename/Service1.asmx", "The web service namespace", "testresponse"
objSoapRequest.StartBody()
objSoapRequest.AddMember "name1", value1
objSoapRequest.AddMember "name2", value2
objSoapRequest.AddMember "name3", value3
objSoapRequest.AddMember "name4", value4
objSoapRequest.AddMember "name5", value5
objSoapRequest.AddMember "name6", value6
objSoapRequest.EndBody()
objSoapRequest.SendRequest()

'to pull values from the response
somevar = objSoapRequest.Extract("bAllowACH")

One thing to note; you have to know the namespace for this to work.  Otherwise the webservice will not service the request.  The default namespace is "http://tempuri.org/ ".

You should always provide a unique namespace to your webservices. To do this, put the following code right before the class:

<WebService(Namespace:="http://www.MyNamespace.com/webservice/";)>

-rca
I am getting an error at soapRequest.addMember, can u tell me where am i going wrong:

the default namespace fro webservice is :http://tempuri.org/
do i have to add this namespace in the class file too..

<%
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
Set objSoapRequest = New SoapRequest
          objSoapRequest.OpenHTTPConnection "POST", "http://somename/Service1.asmx?wsdl","http://tempuri.org/","GetAllowedTransactions"

         objSoapRequest.StartBody()
          objSoapRequest.AddMember "TestResponse", (Clng(Request.Form("text1")), Cstr(Request.Form("text2")),Cstr(Request.Form("text3")),Cstr(Request.Form("text4")),Cstr(Request.Form("text5")),Cstr(Request.Form("text6")))
          objSoapRequest.EndBody()
          objSoapRequest.SendRequest()

          'Retreive the bSuccess variable
          somevar = objSoapRequest.Extract("TestResponse")

          Set objSoapRequest = nothing
end if
%>
First, you need to put the method name on this line (replace GetAllowedTransactions):

objSoapRequest.OpenHTTPConnection "POST", "http://somename/Service1.asmx?wsdl","http://tempuri.org/","GetAllowedTransactions"


Second, the formatting of the second parameter on your AddMember is not correct.  Assume your webservice signature is similar to the following:

string testresponse ( long lng1, string str2, string str3, string str4, string str5, string str6)

According to this, your AddMember calls would look like this (list them seperately):

objSoapRequest.AddMember "lng1", Clng(Request.Form("text1")
objSoapRequest.AddMember "str2", Cstr(Request.Form("text2")
objSoapRequest.AddMember "str3", Cstr(Request.Form("text3")
objSoapRequest.AddMember "str4", Cstr(Request.Form("text4")
objSoapRequest.AddMember "str5", Cstr(Request.Form("text5")
objSoapRequest.AddMember "str6", Cstr(Request.Form("text6")

-rca
this will be my last question. thanks for ur patience.

now i am able to send the data to service, but i am not able to read the data.

i have 1 method called "checktype": when i send data i get this from webservice: it return an integer 1 or 0.

<?xml version="1.0" encoding="utf-8" ?>
  <int xmlns="http://tempuri.org/">1</int>
------
i know the extract function but i am not able to use it:
for method checktype
 somevar = objSoapRequest.Extract("int")
response.write somevar & "<BR>"

------
i have another method called TestResponse : which sends me an xml string:

<?xml version="1.0" encoding="utf-8" ?>
  <string xmlns="http://tempuri.org/"><ANET><Response>3</Response><Subcode>1</Subcode><Reason>6</Reason><Text>The card number is invalid.</Text><AprvCode>000000</AprvCode><AVS>P</AVS><TransID>0</TransID><InvNumbr></InvNumbr><Descript></Descript><Custid>13218646</Custid><Fname></Fname>ANET></string>
----
how to retrive this data.
for method: TestResponse

somevar = objSoapRequest.Extract("Reason")
 response.write somevar & "<BR>"



ASKER CERTIFIED SOLUTION
Avatar of anderson22
anderson22

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
anderson22....  if you get this, I'm asking a question here that relates to your code:  https://www.experts-exchange.com/questions/21540261/Connecting-to-an-ASP-NET-web-service-with-classic-ASP.html