Building a Stock-Quotes Web Service 来源: 时间:2002-09-12 14:43 作者:AMTeam.org Building a Stock-Quotes Web Service
Saurabh Nandu Level: Beginner Introduction I guess you might be having some information on what Web Services. If not you can read .net101's article about Web Service Part 1 -Basics. In the current article we will be concentrating on building and consuming a Web Service which delivers "Stock Quotes" on the .NET Platform. We will be using Yahoo's free service which provides "Stock Quotes" in CSV (Comma Separated Values) format and wrap it up in our web service. Note: The quotes run around 15 minutes late and should only be used for educational purposes. Building the Web Service We will step through the code to understand the Web Service
programming model in .NET. To write our Web Service you can use any text editor
like notepad to type your code and save it as StockQuote.asmx. <%@ WebService Language="C#" class="DailyStock" %> The first line of our code defines a Web Service and the language used to be "C#". The class attribute is used to denote the class which the Web Service should call and expose. If you are using many classes in your Web Service use this attribute to denote the class that should be called first by the Web Service. using System ; Import the necessary Namespaces. The Namespaces System.Web.Services should be always be imported, remaining Namespaces should be imported on the basis of the requirements of your class. public class DailyStock :
WebService Here we define our public class DailyStock, which extends the System.Web.Services.WebService class. All the classes that you want to expose as Web Services should extend the System.Web.Services.WebServices class. Also the access modifier for the Web Service class should always be public. [WebMethod] Above we define a public Web Method GetQuote. As in classes Web
Methods also have to be declared with the public modifier. The [WebMethod]
attribute renders the underlying method to be exposed to the Web Service. So all
the methods that you want the clients to access should be marked with the
[WebMethod] attribute. string ret; // Create a HttpWebRequest object on the Yahoo
url // Get a HttpWebResponse object from the Yahoo
url // Create a StreamReader object and pass the Yahoo Server
stream as a parameter // Read a single line from the stream (from the server)
// Close the stream to the server and free the
resources. catch(Exception) //Return the Quote or Exception Once the path is constructed we make a HttpWebRequest object and a HttpWebResponse out of the connection string. Next we open up a stream to the Yahoo server using the StreamReader. The StreamReader reads one line from the server since all information that we need is provided by Yahoo in a single line. Finally the stream is closed and the output from Yahoo is returned to the Consumer. Deploying the Web Service Deploying the Web Service is similar to the deployment of a ASP.NET application. If you have your own web server then create a virtual directory called stockquote (you can use any name you want.) and then copy the StockQuote.asmx into this directory. This completes the Web Service deployment. To call the Web Service fire up your browser and type the path to the Web Service i.e. http://localhost/stockquote/StockQuote.asmx. The page you see is automatically generated for you by the Web Services runtime. Type the symbol (like AOL or MS etc) in the textbox provided and click the invoke button. A new browser will pop-up showing you the output of the Web Service. Consuming the Web Service There are basically two ways to consume Web Services. You can either call the Web Service directly through your browser and use it which we have done above. The second method is to use some application to consume it. We will see how to build a Web Application and a Console Application which will consume our Web Service. You can alternatively use Win Form Applications to consume the Web Service. Get the Service Description In order to communicate with the Web Service the client should know what methods are supported by the Web Service and how to communicate with them. Web Services on .NET automatically produce a XML formatted document in SDL (Service Description Language) to enable the clients gain the information they need to communicate with Web Services. Appending ?SDL after the Web Service URL causes to Web Service to generate the SDL document that the clients can use. To view the SDL of our Web Service type http://localhost/stockquote/StockQuote.asmx?SDL in your browser. Build the Proxy Library Applications on the .NET Platform can use Proxy libraries to call methods on a Web Service which makes consuming Web Service very easy. First step in generating a proxy library is to generate a proxy class out of the SDL of the Web Service. The .NET SDK provides a tool called WebServiceUtil.exe which helps us to generate a proxy class. To generate a proxy class of our Web Service start-up your command prompt (ms-dos prompt) and navigate to the directory in which you will be developing you client application. Then enter the following command: WebServiceUtil
/c:proxy The above command generates a C# source code file called DailyStock.cs (Web Service class name is also DailyStock, remember!) in the directory from which you ran this command. Let's see what the arguments mean.
Once we have the proxy class ready, we use the C# compiler to generate a proxy library out of the proxy class we just created in the above step. csc /target:library
/r:System.dll;System.Web.Services.dll;System.Net.dll; Creating a Web Application Consumer We will create a Web Application as our first consumer of the StockQuote Web Service. StockConsumer.aspx <%@ Page language="C#" %> Import the necessary Namespaces. Remember to import the Quotes Namespace too, which is the Namespace of your proxy class <html> // Call the GetQuote method of the proxy class DailyStock and
// Check if the string array returned has more than one
Above in the script tag on the ASP.NET page we first instantiate a instance of the class DailyStock (Web Service). Since we have generated the proxy library calling methods of the Web Service is similar to calling methods on any other library. We call the GetQuote() method of the DailyStock class which returns a string containing the full information of the symbol in a comma separated list. We will restrain our client to display only the current index and the date/time on which the index was recorded. In order to break up the string into different parts we use the Split method of the string class to split the string into parts on the occurrence of every comma. Once we have the spitted array of strings we just set the different labels of the Web Page with the relevant values. Rest of the code <body> <form runat=server > Deploying the Web Application Create a virtual directory named StockConsumer. |
||||||
关键词:
|
相关文章 |