Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Calling a .NET web service from classic ASP

Reply
Thread Tools

Calling a .NET web service from classic ASP

 
 
Julian Hershel
Guest
Posts: n/a
 
      04-15-2004
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


 
Reply With Quote
 
 
 
 
Jiho Han
Guest
Posts: n/a
 
      04-16-2004
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.

"Julian Hershel" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
Matt Berther
Guest
Posts: n/a
 
      04-16-2004
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.

--
Matt Berther
http://www.mattberther.com
 
Reply With Quote
 
Julian Hershel
Guest
Posts: n/a
 
      04-16-2004
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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.
>
> --
> Matt Berther
> http://www.mattberther.com



 
Reply With Quote
 
Matt Berther
Guest
Posts: n/a
 
      04-16-2004
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.

> '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


--
Matt Berther
http://www.mattberther.com
 
Reply With Quote
 
pankajgharge pankajgharge is offline
Junior Member
Join Date: Apr 2009
Posts: 2
 
      04-17-2009
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&_p assword=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>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
InvocationTargetException when calling "new Service()" in Axis web service to call another web service Michael Averstegge Java 0 01-10-2006 11:05 PM
Using an ASP.NET Web Service from classic ASP? Wade ASP .Net Web Services 1 08-03-2005 03:39 AM
Calling a Web Service using Axis, from within an Axis Web Service running under Tomcat hocho888 Java 1 04-29-2005 08:26 PM
Calling a .NET web service from classic ASP w/o soap tookit ryan.mclean@gmail.com ASP .Net 3 01-19-2005 04:15 PM
HELP!! Classic ASP and .net web service - service restarted often! Celia Leung via .NET 247 ASP .Net Web Services 0 04-26-2004 04:23 AM



Advertisments