Monday, July 14, 2008

Calling Webservices from VB Script and Java Script client code - A step by step introduction

We often need ability to call a web service from client scripts itself. In todays Service Oriented Architecture this has become more essential than anything. Recently I faced a requirement of calling web service from VB Script, after some google-ing, I found there is no consolidated easy step by step article any one has written before. Though I did get many code snippets but eventually had to figure out how to make them work with my application and also how the code works.
Thus this articles mainly addresses this issue, I have tried to cover some of the the commonly possible scenarios of a web service call from client script and also considered most of the possible technologies which are available for Microsoft .Net Framework at present.
At the outset I should mention that I have tested my code only for Microsoft .Net Framework web services, this scripts won't work directly without modification with web services of Java and other technologies. The header and SOAP format and other details differ, and thus should be accordingly be modified into the scripts to make them work.
Also this is article is actually only a quick start, a single place to tell you how to do the basics and what are the available options. It is not intended to be extensive by any means and only personal work.
Further: JS and VBS files available here is freely distributable, and I do not endorse anything I said here for commercial purposes but only for learning purposes.

Introduction:

A script which runs under the browser is often limited to what is already rendered into the browser and can typically only manipulate those objects. Script do not have direct access to the server side code/objects. Often when something needs to be updated into page a re-load is necessary to re-render and update the control with new information. But this approach is costly and leads to an unnecessary round trip. Asynchronous calls to the server object comes to rescue here. The technology AJAX is wholly based on this requirement. You might have notices how gmail achieves such gigantic work without reloading the whole page even once. All this is archived thanks to the ability of making Asynchronous calls. Microsoft has a inbuilt trusted component called XMLHTTP which will give this ability to make asynchronous calls to client side script. Thus in a way XMLHTTP object is at the heart of AJAX technology. XMLHTTP object can be similarly be used to make remote calls to the web service. Web service is an endpoint which provides some service to be called by other remote code, typically they are web methods. Web service follows its own protocol called SOAP which stands above the HTTP protocol layer. What typically happens is that an HTTP request GET or POST is formed out of the thing called SOAP envelope. That is the SOAP envelope is the body part of the HTTP request and thus is important in making a asynchronous call to web service from client side. Although web service may also be called only through simple HTTP but only if the server supports it.

Through XMLHTTP object and SOAP protocol.

Let us first assume existence of a Web Service in local machine having URI http://localhost/MyWebService/Service.asmx having a Web method having MyWebMethod which takes one parameter named sParameter and returns a single string. The web service is written in C# using Visual Studio 2005.



VB Script

' WebService URL
Const serviceUrl = "http://localhost/MyWebService/Service.asmx"

Const sParameter = "3B759400006202020546" 'String parameter

Dim nodes
Dim oXml

Dim xmlDoc

Dim strXmlData

' Create HTTP Object, this will make HTTP calls.
Set oXml = CreateObject("MSXML2.XMLHTTP")

' We need to parse the returned data from WebService, which is in XML form.
Set xmlDoc = CreateObject("Microsoft.XMLDOM")

' Make SOAP Header and envlope here. In .Net WebService, if you call the
' WebService from local machine, it will show the SOAP Header and Envlope
' and Body which will be necessary to call a particular WebMethod in the
' help/description section of ASMX file. This can used excatly without an
' further modification to call a WebMethod, avoiding the need of
' understanding the SOAP details seperately. I have used the same
' information below.


' Use double qoutes to escape the qoutes in a string.
strXmlData = "<?xml version=""1.0"" encoding=""utf-8""?>"

strXmlData = strXmlData & "
<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" >"

strXmlData = strXmlData & "
<soap:Body>"
' Note this whole XML wrapped is what .Net uses to call the WebService
' and calls it SOAP, MyWebMethod is actual WebMethod name.

strXmlData = strXmlData & " <MyWebMethod
xmlns=""http://www.tempuri.com/"" >"

' The below parameter is only a string, if more complex parameter needs to ' be passed, the
' WebService description of the Method it self will show
what is the XML needed to serialize the
' complex Parameter.


strXmlData = strXmlData & " <sParameter>" & sParameter & "</sParameter>"

strXmlData = strXmlData & " </MyWebMethod>"

strXmlData = strXmlData & " </soap:Body>"

strXmlData = strXmlData & "</soap:Envelope>"

oXml.open "POST", strUrl, False

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

oXml.send strXmlData

' Check whether HTTP OK. You can check other conditions if you want and
' handle error such as 404.

If oXml.Status = "200" Then

If xmlDoc.loadXML(oXml.responseText) Then

' The response of the call to the WebMethod will be enclosed in an tag of
' the form WebMethodName + Response, that is concatation of the string
' 'MyWebMethod' and 'Response', yielding to 'MyWebMethodResponse'

nodes = xmlDoc.documentElement.selectSingleNode("//MyWebMethodResponse").attributes(0).text

' You may write other complex parsing logic using XMLDOM here to parse more ' complex returned object into object form. Your WebMethod may itself do
' its custom serialization, which you need to de-serialize it here, and
' assign it to a equivalent object in VB Script and use it.

MsgBox nodes

End If

Else

MsgBox "Error: " & oXml.Status & " - " & oXml.statusText

End If

Java Script

var strUrl = "http://localhost/MyService/Service.asmx";
var sATR = "3B759400006202020546";

var oXml;

var xmlDoc;

var strXmlData;


oXml = new ActiveXObject("MSXML2.XMLHTTP"); // Create HTTP ActiveX object instance

xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); // Create XMLDom ActiveX object instance

strXmlData = ""; // Notice escape sequence for quotes

// Make soap envlope, follow Listing 1 for detail discription.

strXmlData = strXmlData + "<?xml version=""1.0"" encoding=""utf-8""?>";

strXmlData = strXmlData + "<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" >";

strXmlData = strXmlData + " <soap:Body>";

strXmlData = strXmlData + " <MyWebMethod
xmlns=""http://www.tempuri.com/"" >";

strXmlData = strXmlData + " <sParameter>" & sParameter & "</sParameter>"

strXmlData = strXmlData + " </MyWebMethod>";

strXmlData = strXmlData + " </soap:Body>";

strXmlData = strXmlData + "</soap:Envelope>"
;

oXml.open( "POST", strUrl, false);

oXml.setRequestHeader("Content-Type", "text/xml");

oXml.send(strXmlData);

if(oXml.Status == "200")
{

if (xmlDoc.loadXML(oXml.responseText))
{

var nodes;
nodes = xmlDoc.documentElement.selectSingleNode("//GetCardMapResponse").attributes(0).text;


alert( nodes);

}
}
else
{
alert( "Error: " + oXml.Status);
}




Through XMLHTTP object and HTTP protocol only.
VB Script

Java Script

By Using WebService.htc behaviour for Javascript

Microsoft Web Services Enhancements (WSE)

Tuesday, July 1, 2008

Metaphysics, Reality or is it Meta-Reality!

Meta Meta Meta.... Well meta literally and logically means anything which deals with a subject from a higher level of abstraction... like meta-subject, now the bold part is a place holder, like 'meta-physics': a physics about the physics and so on. So what the hell is meta-reality..... well I assume you would know that it is a 'reality about reality'. But of course I can't think of any concrete examples where we can term the subject as reality about reality.
Here is a funny memory, I just remembered something from my college days.... There was a Girl, whome whenever I use to ask 'Are you in love by any chance', she use to say that 'I guess I am in love with love'.... Huh... now I understand... she was in 'meta love'....
Did you notice my Blog's name.... Well lets see... 'Meta-Muse', it's a muse about musing.... Ahaa what an enlightenment... I am starting to get headache.....

Let me return to my original title... meta physics... going by the above rules.. you would say metaphysics is physics about physics.... But what is that?..... well lets see... String theory.... is it meta physics?... Who said?... That is physics! But String theory does shapes the Physics we know... and thus it influences what the current physics is like... and thus logically it's metaphysics.... am I wrong there? I think I am wrong there! Well when you need to decide what is metaphysics... before doing that you need to decide what is Physics, once you decided that what it is, what all it covers, then anything outside that, anything outside that domain of existing knowledge of all physics is meta physics, that which resides out side the known Physics but it talks about the Physics, influences it, ponders about it, builds theory about Physics itself. Huh... that is complicated....

An interesting one, while saving this blog before taking a break, the blogger software asked for 'Labels for this post'.... you guessed it I entered there 'meta-philosophy'.

So what is meta-philosophy? I think it is about philosophy about philosophy! Well that is philosophy about 'philosophy about philosophy'.
Like I said for the case meta-physics, if we think about this meta business more generally, that is to cover all the meta-meta... levels, that is a thought which would characterize the general meta-meta... recursion system then I would think that whenever we need to go below/above one more level of recursion we need to first decide what are the boundaries of each subject, so that we can safely decide what is meta with respect of a particular subject.
Now I think you should know that in the last paragraph I was doing meta-philosophy. Why, what are the subjects.? In the last discussion the subject was the 'Process of going from one level of subject to it's meta level'. You may again notice that the word 'subject', it can be considered as a place holder where any concrete subject may be substituted.
So we see that this met-meta.... business is all about abstraction, the levels of abstraction much like levels of 'meta' can be an infinite recursion, off course if we can figure out what is the next higher level of abstraction with respect of a particular subject then we can traverse one more level of recursion.
Human mind, I think is definitely limited in terms of levels of abstraction which it can perceive with out going insane. Mathematics is often but all about abstraction, but well off course the abstraction done while designing a Software system and the abstractions involved in various provinces of Mathematics are much different in complexity and depth. I always had a tough time with Maths. Through out my schooling I generally managed to avoid the dark areas of my understanding of Mathematics quite safely. I hated to do the drills, and often the notion what is given in our schooling is that Maths is all about drills, practice and nothing else.

I hated anything which I did not find physical significance for. Things which were abstract generally won't get absorbed in my mind. The encouragement of involving yourself in constant drill without caring a bit about understanding in schools proved to be detrimental to my metamathematical concepts. If I remember the subject I use to love was offcourse physics, since I could almost always find a physical significance immediately. I liked mechanics and dynamics a lot, for the same reason that what you read in the books can be readily verifiable by observing the motions of earthly objects. Maths often scored low on this characteristics and thus I kept on avoiding them. Calculus was good enough for me, since it was part of Physics, I can well say that I learned Calculus from my Physics books only.
Often people have difficulty understanding such concepts as Complex numbers, why any number power to the zero is one? Why negative multiplied by negative is positive. I guess all this is about understanding the abstraction, the meta-business. I always had difficulties understanding why the set theory was necessary, unfortunately in our school level of studies we don't see any real applications of set theory. Set theory is by and far an higher level of abstraction which is required to analyze such concrete sets as Real Numbers, Integers etc. It is a subject which allow out to draw out more generic characteristics of such concrete sets as N, I, Z etc. Thus set theory in a sense is a meta theory. And here I missed it again.

Well so back to where I started, the meta business, I wrote that I have no real instance of thought which I can say is 'Meta-Reality', I am having a feeling that this is a deep question. May be in a world like Matrix by Wachowski Brothers, it is valid to think abut 'Meta-reality'.

Ahaa.... I am going insane with all these recursive depths, or may be meta-insane.
Please leave comments about your encounters of manifestation of this meta systems.