Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET

Calling a WebService from ASP3.0 and JavaScript

Rate me:
Please Sign up or sign in to vote.
4.66/5 (12 votes)
23 Jun 20053 min read 196.9K   40   23
Calling a WebService from ASP3.0 and JavaScript

Introduction

I don’t know whether anybody has faced this problem or not, but we were stuck deep in a hole. The 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. The 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.

  1. Using client-side JavaScript
  2. Using Microsoft SOAP Toolkit 2.0
  3. 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
<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
    'Create an object of Soap Client
    Set oSOAP = Server.CreateObject("MSSOAP.SoapClient")
    oSOAP.ClientProperty("ServerHTTPRequest") = True
    'Initialize the Web Service
    oSOAP.mssoapinit("http://localhost/Add/SimpleAddService.asmx?wsdl")
    'Invoke the Web Service
    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
<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 leaving a comment below as I am really looking forward to it.

That’s the key.

VBScript
DataToSend="Val1="&Request.Form("text1")&"&Val2="&Request.Form("text2")

This is the line where we are constructing the data that is to be sent in the HTTP POST.

VBScript
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.

VBScript
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.

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAjax solution Pin
semenoff18-Sep-07 8:40
semenoff18-Sep-07 8:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.