Introduction
I don�t know whether any body has faced this problem or not but we were stuck deep in a hole. Problem was to create a web service and consume it from ASP.NET and ASP 3.0.
Creating the web service and consuming it in ASP.NET was the easy part because .NET provides inherent support for it. Interesting part was to consume the same web service in ASP 3.0.
After much digging, I found three completely different ways in which this could be done.
- Using client-side JavaScript.
- Using Microsoft SOAP Toolkit 2.0.
- Using the .NET Framework and MSXML parser (the approach I used).
Using Client-Side JavaScript
I came across a few very good resources for this approach. Below are the links:
Using Microsoft SOAP Toolkit 2.0
This technique is the most common and prevalent way and easiest to use. But Microsoft recommends not to use it. Please check this link.
What they say is:
�The SOAP Toolkit has been decremented by the .NET Framework. SOAP Toolkit support will be retired in April 2005 with extended support retiring in April 2008.�
Still for the uninitiated ones, here is the �how to� and the �code� for using this technique:
<html>
<head>
<title>Calling a webservice from classic ASP, using the SOAP Toolkit</title>
</head>
<body>
<%
�Only to process a Post Request
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
Dim oSOAP
Set oSOAP = Server.CreateObject("MSSOAP.SoapClient")
oSOAP.ClientProperty("ServerHTTPRequest") = True
oSOAP.mssoapinit("http://localhost/Add/SimpleAddService.asmx?wsdl")
Response.write(oSOAP.Add(Request.Form("text1"), _
Request.Form("text2")) & "<BR>")
End If
%>
<FORM method=POST name="form1">
Enter the two Values to be Added.<BR>
<INPUT type="text" name="text1">
<INPUT type="text" name="text2">
<BR><BR>
<INPUT type="submit" value="Add" name="submit1">
</form>
</body>
</html>
Using MSXML Parser
This technique leverages the .NET framework and MSXML parser for calling the Web Service in ASP 3.0. Below is the sample code. You need to install the .NET framework which is free to download.
<html>
<head>
<title>Calling a webservice from classic ASP</title>
</head>
<body>
<%
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
Dim xmlhttp
Dim DataToSend
DataToSend="Val1="&Request.Form("text1")&"&Val2="&Request.Form("text2")
Dim postUrl
postUrl = " http://localhost/Add/SimpleAddService.asmx/Add"
Set xmlhttp = server.Createobject("MSXML2.XMLHTTP")
xmlhttp.Open "POST",postUrl,false
xmlhttp.setRequestHeader "Content-Type", _
"application/x-www-form- urlencoded"
xmlhttp.send DataToSend
Response.Write(xmlhttp.responseText)
End If
%>
<FORM method=POST name="form1">
Enter the two Values to be Added<BR>
<INPUT type="text" name="text1">
<INPUT type="text" name="text2">
<BR><BR>
<INPUT type="submit" value="Add" name="submit1">
</form>
</body>
</html>
I will need to explain a few things over here.
HTTP POST
The following are a sample HTTP POST request and response. The placeholders shown need to be replaced with actual values.
POST /Add/SimpleAddService.asmx/Add HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length
Val1=string&Val2=string
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">string</string>
What you see above is the way you need to make a request and get a response to the web service if you use HTTP POST. So, why do I need to explain this to you. It�s because, if we want to invoke the web service using XMLHTTP then we need to construct the Request message that is to be posted, and only after the successful post can we expect a correct response.
In the middle I missed an important thing. This question may have crossed your mind that why do we need the POST method and why not the GET method? Yes, we can very well invoke a web service using GET method, but I can't pass parameters to the WEBMETHOD
using GET. I don�t understand how it's possible but still if anyone is aware of a technique then please do update me about that by dropping a mail at ipsit.naik@gmail.com as I am really looking forward to it.
That�s the key.
DataToSend="Val1="&Request.Form("text1")&"&Val2="&Request.Form("text2")
This is the line over where we are constructing the data that is to be sent in the HTTP POST.
xmlhttp.Open "POST",postUrl,false
This line indicates that the method to be used is POST and the postUrl
is the URL to which the request has to be sent.
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
We set the request header over here setting the Content-Type.
And then the data is posted. That�s it and finally we use responseText
to get the text returned from the web service. If you want, you can very well set the response content type to be XML and then get the data in XML form by using xmlhttp.responseXml
. It all depends on your needs. I have tried my best to lay out what I understand of this particular technique.