Calling Web Services in classic ASP
- by cabhilash
Last
day my colleague asked me the provide her a solution to call the Web
service from classic ASP. (Yes Classic ASP. still people are using
this :D )
We
can call web service SOAP toolkit also. But invoking the service
using the XMLHTTP object was more easier & fast.
To
create the Service I used the normal Web Service in .Net 2.0 with
[Webmethod]
public
class
WebService1
: System.Web.Services.WebService
{
[WebMethod]
public
string
HelloWorld(string
name){return
name + "
Pay my dues :) ";
// a reminder to pay my consultation fee :D}
}
In
Web.config add the following entry in System.web<webServices><protocols><add
name="HttpGet"/><add
name="HttpPost"/></protocols></webServices>
Alternatively,
you can enable these protocols for all Web services on the computer
by editing the <protocols> section in Machine.config. The
following example enables HTTP GET, HTTP POST, and also SOAP and HTTP
POST from localhost:
<protocols>
<add
name="HttpSoap"/>
<add
name="HttpPost"/>
<add
name="HttpGet"/>
<add
name="HttpPostLocalhost"/>
<!--
Documentation enables the documentation/test pages -->
<add
name="Documentation"/>
</protocols>
By
adding these entries I am enabling the HTTPGET & HTTPPOST (After
.Net 1.1 by default HTTPGET & HTTPPOST is disabled because of
security concerns)The
.NET Framework 1.1 defines a new protocol that is named
HttpPostLocalhost. By default, this new protocol is enabled. This
protocol permits invoking Web services that use HTTP POST requests
from applications on the same computer. This is true provided the
POST URL uses http://localhost, not http://hostname. This permits Web
service developers to use the HTML-based test form to invoke the Web
service from the same computer where the Web service resides.
Classic
ASP Code to call Web service
<%Option
Explicit
Dim
objRequest, objXMLDoc, objXmlNode
Dim
strRet, strError, strNome
Dim
strName
strName=
"deepa"
Set
objRequest = Server.createobject("MSXML2.XMLHTTP")
With
objRequest
.open
"GET",
"http://localhost:3106/WebService1.asmx/HelloWorld?name="
& strName, False
.setRequestHeader
"Content-Type", "text/xml"
.setRequestHeader
"SOAPAction",
"http://localhost:3106/WebService1.asmx/HelloWorld"
.send
End
With
Set
objXMLDoc = Server.createobject("MSXML2.DOMDocument")
objXmlDoc.async
= false
Response.ContentType
= "text/xml"
Response.Write(objRequest.ResponseText)
%>
In
Line 6 I created an MSXML XMLHTTP object.
Line
9 Using the HTTPGET protocol I am openinig connection to WebService
Line
10:11 – setting the Header for the service
In
line 15, I am getting the output from the webservice in XML Doc
format & reading the responseText(line 18).
In
line 9 if you observe I am passing the parameter strName
to the
Webservice You can pass multiple parameters to the Web service by
just like any other QueryString Parameters.
In
similar fashion you can invoke the Web service using HTTPPost. Only
you have to ensure that the form contains all th required parameters
for webmethod.
Happy
coding !!!!!!!