Solved

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

Posted on 2004-04-07
12
624 Views
Last Modified: 2008-02-03
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.
0
Comment
Question by:sumitarigapudi
  • 5
  • 4
12 Comments
 
LVL 4

Expert Comment

by:anderson22
ID: 10778633
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
0
 

Author Comment

by:sumitarigapudi
ID: 10778955
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>")

      
      %>
0
 
LVL 4

Expert Comment

by:anderson22
ID: 10779007
xml parser 4, the soap toolkit is not necessary

-rca
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:sumitarigapudi
ID: 10779196
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.
0
 
LVL 4

Expert Comment

by:anderson22
ID: 10779257
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
0
 

Author Comment

by:sumitarigapudi
ID: 10783387
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
%>
0
 
LVL 4

Expert Comment

by:anderson22
ID: 10786179
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
0
 

Author Comment

by:sumitarigapudi
ID: 10786697
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>"



0
 
LVL 4

Accepted Solution

by:
anderson22 earned 125 total points
ID: 10787292
my extract function attempts to read the return type based on the name of the variable.  If the return variable starts with b,i,s the extract function attempts to cast the value to boolean, integer or string respectively.  This may be causing the trouble.  Just remove or comment the following the following lines from the extract function:

                        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

Also, from your first method "checktype", I'm not sure if the extract function will work for this type of return.  My webservices all return structures, so I don't have thing to test this with.  Regarding the TestResponse method, the following code should work:

somevar = objSoapRequest.Extract("Response")

-rca
0
 

Expert Comment

by:hb2web
ID: 14755034
anderson22....  if you get this, I'm asking a question here that relates to your code:  http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_21540261.html
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Prevent URL from printing on header 5 68
CSS Question.. 3 85
Generate report pulling data (links) from three tables 31 64
Javascript to allow login/password authorization 4 44
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now