Feb 17 2006

Consume .Net WebService in ASP 3.0

Category: ASP.NET 1.xBil@l @ 19:12

I had the need this week to integrate a .Net WebService with an old ASP 2.0 web application at work.

I passed through tough times in fact, and that is why, I want to post here to help everyone who might need to consume a .Net WebService in ASP 2.0 applications.

The following code would access the .Net WebService by sending a request and receive a response:

 

Function EncryptText (Value)
    Dim DataToSend, xmlhttp, postUrl, XMLDOM, XMLNode
    DataToSend="value="& Value
    postUrl = "http://mysite.com/Security.asmx/EncodeString"
    Set xmlhttp = server.Createobject("MSXML2.XMLHTTP")
    xmlhttp.Open "POST",postUrl,false
    xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"

    xmlhttp.send DataToSend
    
    Set XMLDOM = Server.CreateObject("Microsoft.XMLDOM")
    XMLDOM.Load(xmlhttp.responseBody)
    Set XMLNode = XMLDOM.SelectSingleNode("//string")
    
    If Not XMLNode Is Nothing Then
        EncryptText = XMLNode.Text
    End If        
End Function


First of all, we prepare the URL of the WebSerivce, then require a POST request, using the MsXml component that comes from Microsoft. We then set the RequestHeader of the request to "application/x-www-form-urlencoded", which is needed to be able to send the QueryStrings which are the parameters to the method being called in the WebService.

Then, we receive the Response from the webservice, fill it in an XML document, and then use XPath to get the returned result.

Web Services in .NET 1.1 has by default HttpGet and HttpPut turned off, contrary to .NET 1.0 and that is for sure a security issue.

You need to turn on the HttpGet and HttpPut inside the web.config of the WebService itself to be able to execute the code above, since we are sending a POST request.

<configuration>
    <system.web>
    <webServices>
        <protocols>
            <add name="HttpGet"/>
            <add name="HttpPost"/>
        </protocols>
    </webServices>
    </system.web>
</configuration>

If your ASP 3.0 application that is calling the webservice is stored locally, then no need to enable anything, it works just fine.

Hope that helps you.

Regards

Tags:

Comments are closed