Web services, the Web service, are Microsoft. A very important concept in the net strategy. Its purpose is to transform the Web site into a design web site that integrates organization, application, service, and equipment, so that the Web site is no longer in a passive position.
This article describes how to create and use a Web service that provides stock quotes on the. NET platform. We will use Yahoo's free service to provide stock quotes in CSV (comma-separated values) format and include it in our web services.
Note: This quote service routine runs for about 15 minutes and is used for instructional purposes only.
Setting up a Web service
The following step-by-step code is used to help you understand the programming patterns of Web services in. NET. We can use any text editor such as Notepad to write the Web service routine code here, and finally save the file as Stockquote.asmx. Please note that all Web service files are saved with the extension *.asmx.
The first line of code defines a WEB service, and the language used is C #. The class attribute is used to indicate the classes that the Web service should call and use. If you use many classes in a Web service, you should use this property to indicate the class that the Web service should call first.
Using System;
Using System.Web.Services;
Using System.Net;
Using System.IO;
Using System.Text;
The above code is responsible for introducing the necessary namespaces. Please remember to always introduce the System.Web.Services namespace. The reserved namespace is introduced, depending on the needs of the class.
public class Dailystock:webservice
{
......
....
}
Here we define the public class as Dailystock, which extends the System.Web.Services.WebService class. All classes that want to be exposed as WEB services should extend the System.Web.Services.WebServices class. In addition, the access modifiers for WEB services are always public.
[WebMethod]
public string GetQuote (string symbol)
{
........
........
}
We have defined a common Web method getquote. As with similar definitions, WEB methods are declared using the modifier of public. The [WebMethod] attribute presents some potential methods that will be used in Web services, and all methods that the client accesses should be marked with the [WebMethod] attribute. The GetQuote method accepts a string input parameter that contains the quotation symbol required by the consumer. This method returns a string that contains a stock quote or error message.
string ret;
Try
{
The Path to the Yahoo quotes Service
string fullpath = @ "http://quote.yahoo.com/d/quotes.csv?s=" +symbol+ "&f=sl1d1t1c1ohgvj1pp2owern&e=.csv";
Create a StreamReader object and pass the Yahoo Server stream as a parameter
StreamReader strm = new StreamReader (WEBRESP. GetResponseStream (), encoding.ascii);
Read a single line from the stream (from the server)
We Read only A and since the Yahoo server returns all the
Information needed by us in just one line.
Ret= STRM. ReadLine ();
The "close" stream to the "server" and "free".
Strm. Close ();
}
catch (Exception)
{
If exception occurred inform the user
Ret= "Exception occurred";
}
File://Return the Quote or Exception
return ret;
The above is the content of the GetQuote method. Here a Try-catch module is used to intercept possible errors in the process of getting stock quotes from Yahoo. A string variable is declared inside the Try-catch module, which holds the full path to the Yahoo service, and the user-supplied symbol string variable is added to the connection string.
After the path is established, you construct a HttpWebRequest object and a HttpWebResponse object from the connection string. Next, use StreamReader to open a stream to Yahoo server. StreamReader read a row from the server, Yahoo provides us with all the information we need in one line. Finally, the stream is closed and Yahoo's output information is returned to the user.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.