The first step in developing the Yahoo Quote Engine is creating the web service. The web service will be responsible from grabbing the data from Yahoo and parsing this to create a friendly structure. In this application I'll use ASP.Net with C#, but web services can easily be developed using VB.Net, Java and Code fusion.
We can start our web service by creating a file named YahooQuoteWebService.asmx in the editor of your choice. I'll use DreamWeaver MX.
Once the file is created we'll declare the contents of the file as a web service using C# and also provide the class name with its namespace.
<@ WebService Language="c#" class="outthebox.web.services.YahooQuoteWebService" >
Next, let's build the structure for our web service by defining the namespaces to use, providing a namespace where our web service will sit, and defining our service as a public class that implements the WebService object.
using System.Collections;
using System.Web.Services;
using System.Net;
using System.IO;
namespace outthebox.web.services
{
[WebService(Namespace="outthebox.web.services")]
public class YahooQuoteWebService : WebService
{
}
}
With our class declared we can implement the one and only method for this web service GetQuote( string ). This method will handle the logic involved in getting our quotes from Yahoo.
[WebMethod(Description="Using stock symbol gets delayed stock information from Yahoo.",EnableSession=false)]
public string[] GetQuote( string symbol )
{
string url; //stores url of yahoo quote engine
string buffer;
string[] bufferList;
WebRequest webRequest;
WebResponse webResponse;
url = "http://quote.yahoo.com/d/quotes.csv?s="+symbol+"&d=t&f=sl1d1t1c1ohgvj1pp2wern";
webRequest = HttpWebRequest.Create( url );
webResponse = webRequest.GetResponse();
using( StreamReader sr = new StreamReader( webResponse.GetResponseStream() ) )
{
buffer = sr.ReadToEnd();
sr.Close();
}
buffer = buffer.Replace( "\"", "" );
bufferList = buffer.Split( new char[] {','} );
return bufferList;
}
The above is a lot of code so I'll take you through the implementation chunk-by-chunk.
First we have to create a connection to Yahoo and grab the quotes. The following code grabs a comma delaminated file from Yahoo using the symbol variable and stores the results in a string variable named buffer.
url = "http://quote.yahoo.com/d/quotes.csv?s=" + symbol + "&d=t&f=sl1d1t1c1ohgvj1pp2wern";
Creates a connection to the url and stores our results in the string buffer array.
webRequest = HttpWebRequest.Create( url );
webResponse = webRequest.GetResponse();
using( StreamReader sr = new StreamReader( webResponse.GetResponseStream() ) )
{
buffer = sr.ReadToEnd();
sr.Close();
}
Once we have the results from Yahoo we need to break it up and store the results in a string array. This code calls the string method Split( char[] ) to separate the buffer string into chunks using the comma as the delaminator. The method returns a string[] (string array) with quotes details.
buffer = buffer.Replace( "\"", "" );
bufferList = buffer.Split( new char[] {','} );
return bufferList;
Now that you have a working web service, place it on your web server and go to the address in a web browser. You should see Microsoft's web service page with information on how to access this web service. If you don't see the page make sure ASP.Net is installed properly on your web server and check your code for errors.