Calling a .NET web service from classic ASP

Discussion in 'ASP .Net' started by Julian Hershel, Apr 15, 2004.

  1. 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
    Julian Hershel, Apr 15, 2004
    #1
    1. Advertising

  2. Julian Hershel

    Jiho Han Guest

    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" <> wrote in message
    news:...
    > 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
    >
    >
    Jiho Han, Apr 16, 2004
    #2
    1. Advertising

  3. Julian Hershel

    Matt Berther Guest

    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
    Matt Berther, Apr 16, 2004
    #3
  4. 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" <> wrote in message
    news:%...
    > 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
    Julian Hershel, Apr 16, 2004
    #4
  5. Julian Hershel

    Matt Berther Guest

    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
    Matt Berther, Apr 16, 2004
    #5
  6. Julian Hershel

    pankajgharge

    Joined:
    Apr 17, 2009
    Messages:
    2
    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>
    pankajgharge, Apr 17, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick
    Replies:
    2
    Views:
    649
    Steven Cheng[MSFT]
    Oct 1, 2004
  2. Replies:
    3
    Views:
    11,678
  3. Jorge Luzarraga Castro
    Replies:
    0
    Views:
    519
    Jorge Luzarraga Castro
    Nov 11, 2005
  4. Celia Leung via .NET 247

    HELP!! Classic ASP and .net web service - service restarted often!

    Celia Leung via .NET 247, Apr 26, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    277
    Celia Leung via .NET 247
    Apr 26, 2004
  5. Wade

    Using an ASP.NET Web Service from classic ASP?

    Wade, Aug 2, 2005, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    279
    Manohar Kamath
    Aug 3, 2005
Loading...

Share This Page