Calling a .NET web service from classic ASP

J

Julian Hershel

Hi.

I have a very simple web service that I am trying to call from a classic ASP
page. The web service project and the ASP page are both on my development
machine. That's the code of my web service:

<WebService(Namespace:="http://tempuri.org/")> _
Public Class TextData
Inherits System.Web.Services.WebService

<WebMethod(Description:="Simple method")> _
Public Function GetName(ByVal sName As String) As String
Return "The name is: " & sName
End Function

End Class

And in my ASP page I have the following code:

<%Option Explicit
Dim objRequest, objXMLDoc, objXmlNode, strRet, strError
Set objRequest = Server.createobject("MSXML2.XMLHTTP")

With objRequest
.open "GET", "HTTP://tempuri.org/TextData/TextData.asmx", False
.setRequestHeader "Content-Type", "text/xml"
.setRequestHeader "SOAPAction", "HTTP://tempuri.org/GetName"
.send
End With

Set objXMLDoc = Server.createobject("MSXML2.DOMDocument")
objXmlDoc.async = false

If objXMLDoc.LoadXml(objRequest.ResponseXml.Xml) Then
Set objXMLNode = objXMLDoc.SelectSingleNode("GetNameResponse")

If Not objXMLNode Is Nothing then
strRet = objXMLNode.NodeTypedValue
Response.Write("Response: " & sRet)
End If
Else
strError = objXMLDoc.parseError.reason
Response.Write("Error: " & strError)
End If

%>


When I run the ASP page, I receive the error: XML document must have a top
level element.

What do I need to do in order to consume this web service in a classic ASP
page? And why do I need to use tempuri.org instead of localhost, in my
Request?

Thank you,
Julian
 
J

Jiho Han

I'm not an expert on SOAP, in fact, I don't know much about it but it looks
like you're missing SOAP body.
You'll need SOAP body in the request body in addition to the SOAPAction in
the request header.

you don't have to use tempuri.org if you don't want to. You gave your
webservice the namespace tempuri.org. Change it to whatever you want and
make sure you match it in your request.
 
M

Matt Berther

Hello Julian,
What do I need to do in order to consume this web service in a classic
ASP page? And why do I need to use tempuri.org instead of localhost,
in my Request?

tempuri.org is actually the namespace that is used for the web service. Your problem arises from this line:

.open "GET", "http://tempuri.org/TextData/TextData.asmx", False

This line should actually read (assuming your webservice is hosted on the local machine):
.Open "GET", "http://localhost/TextData/TextData.asmx", False

Also, since your method is expecting a string you will want to pass this along on the querystring (ie: TextData.asmx?name=Foo or use a POST method to pass the parameter along.
 
J

Julian Hershel

Hi Matt.

I changed my ASP page as you told me and it worked, thanks. That's my final
code but I would like your help one more time:

<%Option Explicit
Dim objRequest, objXMLDoc, objXmlNode
Dim strRet, strError, strNome

strName = "Julian"
Set objRequest = Server.createobject("MSXML2.XMLHTTP")

With objRequest
.open "GET", "http://localhost/TextData/TextData.asmx/GetName?sName=" &
strName, False
.setRequestHeader "Content-Type", "text/xml"
.setRequestHeader "SOAPAction", "HTTP://localhost/TextData/GetName"
.send
End With

Set objXMLDoc = Server.createobject("MSXML2.DOMDocument")
objXmlDoc.async = false

Response.Write(objRequest.ResponseXml)

'If objXmlDoc.LoadXml(objRequest.ResponseXml.Xml) Then
' Set objXmlNode = objXmlDoc.SelectSingleNode("GetNameResponse")
' If Not objXmlNode Is Nothing Then
' strRet = objXmlNode.NodeTypedvalue
' Response.Write(strRet)
' End If
'Else
' strError = objXmlDoc.parseError.reason
' Response.write(strError)
'End If
%>

I see the entire response in xml. How can I extract only the sName parameter
from the returned xml? The commented code does not work :-(

Thanks,
Julian



Matt Berther said:
Hello Julian,


tempuri.org is actually the namespace that is used for the web service.
Your problem arises from this line:
open "GET", "http://tempuri.org/TextData/TextData.asmx", False

This line should actually read (assuming your webservice is hosted on the local machine):
Open "GET", "http://localhost/TextData/TextData.asmx", False

Also, since your method is expecting a string you will want to pass this
along on the querystring (ie: TextData.asmx?name=Foo or use a POST method to
pass the parameter along.
 
M

Matt Berther

Hello Julian,

First of all, if you're using .NET 1.1 and have not overridden machine.config to allow HTTP GET requests for your web service, you will need to enable that by adding the following XML to your web.config, under the system.web element:

<webServices>
<protocols>
<add name="HttpGet"/>
</protocols>
</webServices>

Secondly, since youre not using SOAP, you can expect this simple response to be in the format of:

<string xmlns="http://tempuri.org/">The name is: Julian</string>

Hence, you should be able to extract the data that you want with something like:

Set objXmlNode = objXmlDoc.SelectSingleNode("string")

Lastly, since you are not using SOAP, you can safely delete the setRequestHeader "SOAPAction" line.

I've re-created your simple webservice on my development machine here and it does work after making the changes I just detailed.

If you have further questions, please let me know.
 
Joined
Apr 17, 2009
Messages
2
Reaction score
0
Hi,
I am using the same code to consume web service in my ASP page. I am very new to .ASP page. Please verify the code bellow and let me know where I am wrong.

<html>
<head>
<title></title>
</head>
<body>

<%

Dim webServiceUrl, httpReq, node, myXmlDoc

webServiceUrl = "ttp://cpp.cisco.com/cpp/CPPLDAP/CPPLDAP.asmx/AuthenticateCiscoUser?db=1&_username=MyUserName&_password=MyPassword"
Set httpReq = Server.CreateObject("MSXML2.ServerXMLHTTP") '("WinHttp.WinHttpRequest.5.1")

httpReq.Open "GET", webServiceUrl, False
httpReq.Send

Set myXmlDoc =Server.CreateObject("MSXML.DOMDocument")
myXmlDoc.load(httpReq.responseBody)

Set httpReq = Nothing

Set objLst = myxmlDoc.getElementsByTagName("*")
If objLst is Nothing Then
Response.Write("There are no Nodes")
Else
Response.Write("There are nodes")
Response.Write("</BR>Number of nodes in response:" & objLst.length)
'For i = 0 to (objLst.length-1)
'Response.Write(CStr(objLst.item(i).nodeName))
'Next
End If


'Set node = myXmlDoc.documentElement.selectSingleNode("//fullname")
'If Not node Is Nothing Then
'consumeWebService = "<b>No User present in AD</b><font color = blue>" & node.text & "</font>"

'Else
'consumeWebService = "<b>User is present in AD</b>"

'End If

'Response.write(consumeWebService)

%>

</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top