Technical · .NET Development

Problems Invoking a .Net Web Service from a Classic ASP Page

July 10, 2007

.NET Development

Problems Invoking a .Net Web Service from a Classic ASP Page

Although I don't know if it is a very common scenario, there are legacy applications written in classic ASP that begin to integrate with applications in .Net, either as part of a gradual migration process or as part of the process of adding new features to the existing application, in both scenarios a simple way to integrate the ASP application with the new .NET application is to expose the new features of the .Net application by means of web services.

Although there is literature on The Net regarding the way this invocation of the Web Service from the web page should be done, not all the examples point to real scenarios, the objective of this post is to show how this integration should be done for real-world applications.

1. The first problem consists of identifying which are the data types that are going to be transferred between the ASP application and the .NET application, my recommendation is to pass data of basic CRL types, that is, strings, longs, integers. Although it is possible to pass objects represented as XML so they can later be deserialized by the .NET application this complicates the scenario quite a bit and includes new elements of error. (We transferred only strings and created the objects in the .NET application, for which we created the appropriate constructors)

2. The next problem is to identify who really is the client of the service, in most scenarios the client of the service is server-side code, (the code that is between the tags is the server code), it is also possible that the client is one of the JavaScript functions of the page that wants to be invoked. (This is the scenario that led to the existence of AJAX Asynchronous JavaScript And XML). For now we will focus on the scenario where the server-side code is the one that consumes the web service.

3. Now we must select the way the asp application will communicate with the web service, basically there are two options, use SOAP, or use the xml parser (HTTP Post) which contains a little-known object called MSXML2.ServerXMLHTTP that can help us to make these calls. Basically it is a matter of taste, ease and convenience, however since it is necessary to have the SOAP Toolkit installed to be able to use the SOAP objects from classic ASP and that component will run out of support as of March 31, 2008, we prefer to use HTTP Post.

4. Well the next thing is to write the web service code and modify the ASP page so it consumes it, we are not going to go deep into the web service part, it is a class with the WebService decoration with some methods decorated with WebMethod, etc. a simple example can be something like this:


Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
<System.Web.Services.WebService(Namespace:="
http://MyNameSpace/services/MyProduct
> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class WorkFlowServices
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function ServiceTest() As String
Return "En Ejecucion..."
End Function
<WebMethod()> _
Public Function MyMetodoconParametros(ByVal Client As String, ByVal UserID As Long, ByVal IDProcessCase As Long, _
ByVal Sequence As Long, ByVal EntryDataType As String, ByVal IDEntryData As String, ByVal EntryData As String) As Long
...hacer algo, retornar algo.
End Function
End Class

5. Now how to consume this Web Service from the classic ASP page, it is something like this:


<%
Dim xmlhttp
Dim DataToSend
Dim postUrlpostUrl = "http://IPMyServer/Services/WorkFlowServices.asmx/MyMetodoconParametros"
DataToSend="Client=Tester&UserID=1&IDProcessCase=80&Sequence=0" & _&EntryDataType=TestWebService&IDEntryData=0&EntryData=1"
Response.write postURL
Response.write "<br>"
Set xmlhttp = server.Createobject("MSXML2.ServerXMLHTTP")
xmlhttp.Open "POST",postUrl,false
xmlhttp.setRequestHeader "Content-Type","application/x-www-form-urlencoded"
xmlhttp.send DataToSend
Response.Write(xmlhttp.responseText)
%>

The important thing about this code segment, is the way the invocation of the method is built, the postURL variable has the address of the web service as well as the name of the method that is going to be invoked. Then the DataToSend variable has the information of the values that the different parameters that are going to be passed to the web service method take (the gathering of these parameters depends on the type of use and interface that is had with the users but it can be the gathering of a form using request.form or the gathering of the QueryString parameters that invoked the ASP page).

Another important thing is the definition of the header in the line xmlhttp.setRequestHeader “Content-Type”,”application/x-www-form-urlencoded” .

Enjoying this? Get more like it, once a week.

Done, the web service was invoked, now the result can be recovered from one of these properties: responseBody, responseStream, responseText, and responseXML., (we used response.text which returns the whole entity body to us as a stream)

Super Important Tips:

• When your web service client is the ASP code itself (“server side”) it is recommended to use the MSXML2.ServerXMLHTTP object which has been designed and optimized for communications between servers.
• The code above generates an error in most of the web services of framework 2.0, it is an error of type “Request format is unrecognized for URL” or in Spanish “Formato de solicitud no reconocido para la dirección URL” this is because the Get and Post protocols are disabled by omission (default) in framework 2.0 applications for all requests from origins other than Localhost. (That is the reason why the test page of the web services as seen from another machine does not have the Test Service form), therefore in order for the invocation to effectively take place these protocols must be enabled, that is done by including the following lines in the web.config file of the .net web service.


<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
(...)
</system.web>

In this article there is more information about the behavior of the protocols, although it only talks about framework 1.1. They also occur in framework 2.0.

I hope it is useful

Juan Carlos Peláez
MCTS

Keywords: Web Services, ASP Clasic, .Net, Framework 2.0, VB, “Formato de Solicitud no reconocido”,

Need Expert Help?

Let's discuss how I can help with your technical challenges

Schedule a Consultation

Get the next one in your inbox

More like this, once a week. The Digest covers AI systems and agents in one weekly edition.

Confirm by email to complete your subscription. Unsubscribe anytime.

Related reading

More in the Digest — a weekly roundup of AI and agents news — or start with AI Strategy. Want help with this? Tell me about your needs →