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.
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.
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("R EQUEST_MET HOD") = "POST" Then
Dim oSOAP
dim nodesText,nodesTransid, nodesResponse
Set oSOAP = Server.CreateObject("MSSOA P.SoapClie nt30")
Set oDOM = Server.CreateObject("Msxml 2.DOMDocum ent.4.0")
' oDOM.async = false
oSOAP.ClientProperty("Serv erHTTPRequ est") = True
oSOAP.mssoapinit("http://somename/Service1.asmx?wsdl")
'oDOM.loadXML(oSOAP.TestRe sponse(Cln g(Request. Form("text 1")), Cstr(Request.Form("text2") ),Cstr(Req uest.Form( "text3")), Cstr(Reque st.Form("t ext4")),Cs tr(Request .Form("tex t5")),Cstr (Request.F orm("text6 "))))
dim str1
str1 = oSOAP.TestResponse(Clng(Re quest.Form ("text1")) , Cstr(Request.Form("text2") ),Cstr(Req uest.Form( "text3")), Cstr(Reque st.Form("t ext4")),Cs tr(Request .Form("tex t5")),Cstr (Request.F orm("text6 ")))
If Not oDOM.Load(str1) Then
response.write "could not load the file" & "<BR>"
response.end
end if
set aNode = oDOM.documentElement.xmlRe sponse
response.write ("the response is: " & aNode)
Response.write("TestRespon se " & oSOAP.TestResponse(Clng(Re quest.Form ("text1")) , Cstr(Request.Form("text2") ),Cstr(Req uest.Form( "text3")), Cstr(Reque st.Form("t ext4")),Cs tr(Request .Form("tex t5")),Cstr (Request.F orm("text6 "))) & "<BR>")
%>
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("R
Dim oSOAP
dim nodesText,nodesTransid, nodesResponse
Set oSOAP = Server.CreateObject("MSSOA
Set oDOM = Server.CreateObject("Msxml
' oDOM.async = false
oSOAP.ClientProperty("Serv
oSOAP.mssoapinit("http://somename/Service1.asmx?wsdl")
'oDOM.loadXML(oSOAP.TestRe
dim str1
str1 = oSOAP.TestResponse(Clng(Re
If Not oDOM.Load(str1) Then
response.write "could not load the file" & "<BR>"
response.end
end if
set aNode = oDOM.documentElement.xmlRe
response.write ("the response is: " & aNode)
Response.write("TestRespon
%>
xml parser 4, the soap toolkit is not necessary
-rca
-rca
ASKER
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.
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.OpenHTTPCon nection "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("bA llowACH")
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
Set objSoapRequest = New SoapRequest
objSoapRequest.OpenHTTPCon
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("bA
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
ASKER
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("R EQUEST_MET HOD") = "POST" Then
Set objSoapRequest = New SoapRequest
objSoapRequest.OpenHTTPCon nection "POST", "http://somename/Service1.asmx?wsdl","http://tempuri.org/","GetAllowedTransacti ons"
objSoapRequest.StartBody()
objSoapRequest.AddMember "TestResponse", (Clng(Request.Form("text1" )), Cstr(Request.Form("text2") ),Cstr(Req uest.Form( "text3")), Cstr(Reque st.Form("t ext4")),Cs tr(Request .Form("tex t5")),Cstr (Request.F orm("text6 ")))
objSoapRequest.EndBody()
objSoapRequest.SendRequest ()
'Retreive the bSuccess variable
somevar = objSoapRequest.Extract("Te stResponse ")
Set objSoapRequest = nothing
end if
%>
the default namespace fro webservice is :http://tempuri.org/
do i have to add this namespace in the class file too..
<%
If Request.ServerVariables("R
Set objSoapRequest = New SoapRequest
objSoapRequest.OpenHTTPCon
objSoapRequest.StartBody()
objSoapRequest.AddMember "TestResponse", (Clng(Request.Form("text1"
objSoapRequest.EndBody()
objSoapRequest.SendRequest
'Retreive the bSuccess variable
somevar = objSoapRequest.Extract("Te
Set objSoapRequest = nothing
end if
%>
First, you need to put the method name on this line (replace GetAllowedTransactions):
objSoapRequest.OpenHTTPCon nection "POST", "http://somename/Service1.asmx?wsdl","http://tempuri.org/","GetAllowedTransacti ons"
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
objSoapRequest.OpenHTTPCon
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
ASKER
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("in t")
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>0 00000</Apr vCode><AVS >P</AVS><T ransID>0</ TransID><I nvNumbr></ InvNumbr>< Descript>< /Descript> <Custid>13 218646</Cu stid><Fnam e></Fname> ANET></str ing>
----
how to retrive this data.
for method: TestResponse
somevar = objSoapRequest.Extract("Re ason")
response.write somevar & "<BR>"
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("in
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
----
how to retrive this data.
for method: TestResponse
somevar = objSoapRequest.Extract("Re
response.write somevar & "<BR>"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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</str
' </GetAllowedTransactions>
' </soap:Body>
' </soap:Envelope>
'Use the following code
' Set objSoapRequest = New SoapRequest
' objSoapRequest.OpenHTTPCon
' objSoapRequest.StartBody()
' objSoapRequest.AddMember "strCustomerID", strCustomerID
' objSoapRequest.EndBody()
' objSoapRequest.SendRequest
'
' 'Retreive the bSuccess variable
' somevar = objSoapRequest.Extract("bS
'
' 'Retreive the bAllowACH variable
' somevar = objSoapRequest.Extract("bA
'
' ...
'
' 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("Msxml
strSoapBody = ""
strNameSpace = ""
strEndPoint = ""
strMethod = ""
strURL = ""
strStructNameSpace = ""
bRequestSent = false
bResponseReceived = false
bInsideStructure = false
strResponseText = ""
End Sub
Public Sub OpenHTTPConnection(Method,
strMethod = Method
strURL = URL
strNameSpace = NameSpace
strEndPoint = EndPoint
End Sub
Private Sub AddHeader(Header, Value)
objXmlHTTP.setRequestHeade
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,StructNam
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
Extract = Mid(strResponseText,( Instr( Instr(strResponseText,Name
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