Calling Web Services in classic ASP

Posted by cabhilash on ASP.net Weblogs See other posts from ASP.net Weblogs or by cabhilash
Published on Thu, 18 Mar 2010 07:37:00 GMT Indexed on 2010/03/18 7:51 UTC
Read the original article Hit count: 666

Filed under:
|
|

 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

  1. Dim objRequest, objXMLDoc, objXmlNode

  2. Dim strRet, strError, strNome

  3. Dim strName

  4. strName= "deepa"

  5. Set objRequest = Server.createobject("MSXML2.XMLHTTP")

  6. With objRequest

  7. .open "GET", "http://localhost:3106/WebService1.asmx/HelloWorld?name=" & strName, False

  8. .setRequestHeader "Content-Type", "text/xml"

  9. .setRequestHeader "SOAPAction", "http://localhost:3106/WebService1.asmx/HelloWorld"

  10. .send

  11. End With

  12. Set objXMLDoc = Server.createobject("MSXML2.DOMDocument")

  13. objXmlDoc.async = false

  14. Response.ContentType = "text/xml"

  15. Response.Write(objRequest.ResponseText)

  16. %>


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 !!!!!!!

© ASP.net Weblogs or respective owner

Related posts about .NET

Related posts about Web Services