One of the webservice of stock quotes (turn Tenkine)

Source: Internet
Author: User
Tags web services
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. <br>
<br>
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. <br>
<br>
Note: This quote service routine runs for about 15 minutes and is used for instructional purposes only. <br>
<br>
Building Web Services <br>
<br>
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. <br>
<br>
<%@ WebService language= "C #" class= "Dailystock"%><br>
<br>
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. <br>
<br>
Using System;<br>
Using System.Web.Services;<br>
Using System.Net;<br>
Using System.IO;<br>
Using System.Text;<br>
<br>
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. <br>
<br>
public class Dailystock:webservice<br>
{<br>
......<br>
....<br>
}<br>
<br>
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. <br>
<br>
[webmethod]<br>
public string GetQuote (string symbol) <br>
{<br>
........<br>
........<br>
}<br>
<br>
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. <br>
<br>
String ret;<br>
Try<br>
{<br>
The Path to the Yahoo quotes service<br>
string fullpath = @ "http://quote.yahoo.com/d/quotes.csv?s=" +symbol+ "&f=sl1d1t1c1ohgvj1pp2owern&e=.csv"; <br>
<br>
Create a HttpWebRequest object on the Yahoo url<br>
<br>
HttpWebRequest Webreq = (HttpWebRequest) webrequestfactory.create (fullpath);<br>
<br>
Get a HttpWebResponse object from the Yahoo url<br>
<br>
HttpWebResponse Webresp = (httpwebresponse) webreq. GetResponse ();<br>
<br>
Create a StreamReader object and pass the Yahoo Server stream as a parameter<br>
<br>
StreamReader strm = new StreamReader (WEBRESP. GetResponseStream (), encoding.ascii);<br>
<br>
Read a single line from the stream (from the server) <br>
We Read only A and since the Yahoo server returns all The<br>
Information needed by us in just one line.<br>
<br>
Ret= STRM. ReadLine ();<br>
<br>
Close the stream to the server and free the resources.<br>
<br>
Strm. Close ();<br>
<br>
}<br>
<br>
catch (Exception) <br>
<br>
{<br>
<br>
If exception occurred inform the user<br>
<br>
Ret= "Exception occurred";<br>
<br>
}<br>
<br>
File://Return the Quote or exception<br>
<br>
return ret;<br>
<br>
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. <br>
<br>
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.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.