Click here to Skip to main content
Click here to Skip to main content

Calling a WebService from ASP3.0 and JavaScript

, 23 Jun 2005
Rate this:
Please Sign up or sign in to vote.
Calling a WebService from ASP3.0 and JavaScript.

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.

  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>
<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
    'Initaialize 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>
<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.

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

Share

About the Author

ipsit.naik
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
Generalcalling a WEBSERVICE .NET FROM ASP CLASSIC .... the result is a Class PinsussJuanEsteban21-Dec-07 14:18 
GeneralGetting Error - Argument 'Length' must be greater or equal to zero. Pinmembergcdand15-Nov-07 2:26 
Hi All,
 
As per your article, i have coded, but i am getting error as "Argument 'Length' must be greater or equal to zero. " and Status as 500.
 
Pls tell what will be the reason for it.
 
Thanx
 
Gautam
GeneralAjax solution Pinmembersemenoff18-Sep-07 9:40 
Generalerror using MSXML PARSER PinmemberVhakti22-Nov-06 12:58 
NewsA cross browser SOAP client PinmemberMatthias Hertel18-Jul-06 6:54 
QuestionWhat about ATLAS? Pinmemberdlesko8-Mar-06 14:56 
AnswerRe: What about ATLAS? Pinmemberipsit.naik12-Mar-06 3:28 
GeneralRe: What about ATLAS? Pinmemberdlesko12-Mar-06 5:44 
GeneralRe: What about ATLAS? Pinmemberipsit.naik12-Mar-06 6:14 
GeneralNo Longer works in Net 2.0 Pinmemberteddyg24531-Feb-06 18:46 
AnswerRe: No Longer works in Net 2.0 PinmemberJeffrey Kohler4-Apr-06 6:46 
GeneralRe: No Longer works in Net 2.0 PinmemberDelie18-Oct-06 6:51 
GeneralRe: No Longer works in Net 2.0 PinmemberKlok27-Dec-06 23:22 
GeneralRe: No Longer works in Net 2.0 Pinmembersulley5-Dec-06 6:54 
GeneralRe: No Longer works in Net 2.0 PinmemberTruck868-Feb-07 3:28 
GeneralRe: No Longer works in Net 2.0 PinmemberHarish Mokhasania27-Mar-07 8:28 
GeneralRe: No Longer works in Net 2.0 Pinmembermikepc2-Mar-08 2:53 
GeneralUsing MSXML Parser: has a bug: ServerXMHTTP should have been used PinmemberUmut Alev25-Jul-05 22:11 
GeneralRe: Using MSXML Parser: has a bug: ServerXMHTTP should have been used PinmemberUmut Alev25-Jul-05 22:13 
GeneralGreat Article PinmemberRichTeel18-Jul-05 9:25 
GeneralWebServices using GET PinmemberDjDanny3-Jul-05 23:49 
GeneralRe: WebServices using GET Pinmemberipsit.naik12-Mar-06 3:30 
GeneralRe: WebServices using GET PinmemberHITALFvn29-Jun-06 19:09 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 24 Jun 2005
Article Copyright 2005 by ipsit.naik
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid