|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
When I was first introduced with Microsoft .Net Web services, it was quite tricky for me to know on how I can utilize web services that implemented in .Net languages such as C#, VB.Net, in ASP code, and the different web protocols to communicate always confused me. This article will definitely help beginners, who just step in to Web Services world. Assumptions: Reader must know about the architecture of Web services, and how to create web service through Microsoft Visual Studio .Net. In addition, reader must have basic knowledge of XML. IntroductionOverview:Web services allow access to software components through standard protocols such as HTTP and SMTP. Using the Internet and XML, we can now create software components that communicate with others, regardless of language, platform. Comparison between DCOM and Web services: DCOM and Web services are to make distributed components, but main benefit of Web services as compare to DCOM (AKA COM over wire) is that any application on any platform can access the web services as long as it uses standard web protocols, and understands the XML encoded messages. Web service clients (AKA consumer) communicate with the web services, through standard protocols such as HTTP Post, HTTP Get, and SOAP. We demonstrate next how to build client applications that utilize each of these protocols. DescriptionTo understand the concept, we will be creating one simple Web Service Provider first, and then, by using this web service, we will see how it can be consume. Web Service ProviderWeb Services providers implement web services and advertise them so that the clients can discover and make use of the services. Because web services run on top of HTTP, there must be a web server application of some sort on the machine that hosts the web services. This web server application can be Microsoft Internet Information Services (IIS), Apache, or any other program that can understand and process the HTTP protocol. In our examples, we use Microsoft IIS, since that is the only web server currently supported by .NET. Later on, we will see how it can be consume. Below, we describe code for one of the simplest web service, stored in "Service.asmx" file, which containing two web methods:
Of course, access modifier for all web method will be public. Code Snippet using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespace = "http://tempuri.org/">http://tempuri.org/")] public class Service : System.Web.Services.WebService { public Service () { } [WebMethod] public string HelloWorld() { return "Hello World"; }"MsoNormal" style="MARGIN: 0in 0in 0pt"> [WebMethod] public string HelloWorldWithCustomMessage(string s_Msg) { return "Hello World " + s_Msg; } } NOTE: I assumed that IIS server is accessible through http://localhost/ link, and we deploy above created web service under virtual directory called "HelloWorldWS". Web Services ConsumersIn above section, we created Web Service Provider successfully, let see how this web service can use by web clients. HTTP Get Consumer (Web Browser):This consumer is the simplest one. If we point web browser at the web service URL (http://localhost/HelloWorldWS/Service.asmx), it will give us a list of supported Web methods.
To find out more about these methods, click one of them. This brings up a default web service consumer. This consumer, auto generated with reflection, is great for testing your web services' methods. It uses the HTTP GET protocol to communicate with the web service. This consumer features a form that lets you test the method (see figure), as well as descriptions of how to access the method via SOAP, HTTP GET, or HTTP POST (We can use this information as reference, when we call web service through Non .Net consumer).
(Figure – HelloWorldWithCustomMessage Method Testing) Very easy to consume… yeaa? .Net ConsumersHTTP Post Protocol:In the above section, we have seen that creating HTTP Get Consumer is just hitting the URL of the web services. Next, we will see how to write consumer code that can use HTTP Post and SOAP to access web services. Note: We will be using C# code to consume web service. Steps to utilize Web Service: I) Generate web Service proxy code II) Once we created Proxy, our next job is to utilize proxy. To utilize Proxy, first I compile proxy, create DLL file, and refer DLL when I will compile consumer.
You might be thinking, why so many steps are required to follow when we want to utilize web service through consumer code. Practically speaking, these steps required when the consumer code is not a .Net language. But if you want to utilize it for any of Microsoft .Net languages (VB.net, C#, Managed C++, Jscript) and you have Microsoft Visual Studio .Net IDE then you just need to discover and add web service reference. Rest of the task will be taken care by VS.Net IDE - all right, after reading this sentence, I believe 90% reader will appreciate VS.Net IDE, and will not run away from this article :). Non-.Net ConsumersThis section shows how to develop non-.NET web service consumers using HTTP GET, HTTP POST, and SOAP protocols. We have used ASP Non-.Net Language, and those who are interesting to call web service from other Non-.Net language, such as PERL, they must need to find substitute of XMLHTTP component. HTTP Get Protocol:Idea is to use XMLHTTP component to call web page/code available over the Internet, and return result to the caller. Comments: We can also use ASPTear component instead of XMLHTTP, which is available free of charge. Below code snippet is self-explanatory: <% Dim o_XmlhttpCaller ' Create Object of MSXml2.XMLHTTP Set o_XmlhttpCaller = Server.CreateObject("Msxml2.XMLHTTP") ' Check whether object created successfully. If ( o_XmlhttpCaller is nothing ) Then Response.Write("<font color=red size=-1>Unable to create Object - Type: Msxml2.XMLHTTP<BR></font>") Response.Write("Solution: Install MSXML 4.0") End If 'Open method will take three arguments '@Param1 'Method - POST or GET '@Param2 'URL - Uniform Resource Locator ' It must be a valid URL (check by visiting through Iexplore or any browser) '@Params3 'Async - If true then the call would be asynchronous, else Synchronous. o_XmlhttpCaller.open "GET","http://localhost/WSHelloWorld/Service.asmx/ HelloWorldWithCustomMessage?s_msg= Hope you like this article.", False 'Make call to the web service. o_XmlhttpCaller.send 'Okay, play return data according to your need. ' I response back to browser as it is. Response.Write(o_XmlhttpCaller.responseText) ' Time to clean the mess manually:-/ Ahh... can we request Microsoft to build GC for ASP? :) Set o_XmlhttpCaller = nothing %> HTTP Post Protocol:Idea of implementation for HTTP POST and HTTP Get Protocol will remain the same, except few changes done in above code. Here is the changed code lines for you:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||
|
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms
of Use
Last Updated: 17 May 2007 Editor: |
Copyright 2006 by codebased Everything else Copyright © CodeProject, 1999-2008 Web17 | Advertise on the Code Project |