Search This Blog

Monday, April 18, 2011

create stockquote webservice in .net

using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Net;
using System.IO;
using System.Text;

///
/// Summary description for StockQuote
///

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class StockQuote : System.Web.Services.WebService {

public StockQuote () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public string GetQuote(string symbol)
{
string result = null;
try
{
//URL to Yahoo spreadsheet format stock quote server...
string serverURL = @"http://quote.yahoo.com/d/quotes.csv?s=" + symbol + "&f=sl1d1t1c1ohgvj1pp2owern&e=.csv";
char[] delim = { ' ' };
char[] delim2 = { ',' };
string[] symbols = symbol.Split(delim);
string sTemp = @"";
string sContentTemp = @"";
string sContentNew = @"";
string strChar = "";
//We then create a HttpWebRequest object for the stock URL:
HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(serverURL);

//-- and Retrieve HttpWebResponse object from the stock URL using the StreamReader object:

HttpWebResponse webresp = (HttpWebResponse)webreq.GetResponse();
StreamReader strm = new StreamReader(webresp.GetResponseStream(), Encoding.ASCII);

//We then loop through each line from the stream, building our return XML Document string, but first we also "clean out" some of Yahoo's "noise" (quote) characters using the StringBuilder class:
StringBuilder strContent = new StringBuilder("");
sTemp += "";
for (int i = 0; i <= symbols.GetLength(0) - 1; i++) { sContentTemp = strm.ReadLine(); strContent.Append(sContentTemp); for (int k = 0; k <= strContent.Length - 1; k++) { // remove the char from StringBuilder string if contains quite symbol ( \" ) if (strContent[k].ToString() == "\"") strContent.Remove(k, 1); } // now should have clean string, create array of elements string[] contents = strContent.ToString().Split(delim2); // Clear out our StringBuilder for next iteration strContent.Remove(0, strContent.Length); sTemp += "";
sTemp += "" + contents[0] + "";
sTemp += "" + contents[1] + "";
sTemp += "" + contents[2] + "";
sTemp += "";
sTemp += "" + contents[4] + "";
sTemp += "" + contents[5] + "";
sTemp += "" + contents[6] + "";
sTemp += "" + contents[7] + "";
sTemp += "" + contents[8] + "";
sTemp += "" + contents[9] + "";
sTemp += "" + contents[10] + "";
sTemp += "" + contents[11] + "";
sTemp += "" + contents[13] + "";
sTemp += "" + contents[14] + "";
sTemp += "" + contents[15] + "";
sTemp += "" + contents[16] + "";
sTemp += "
";
result += sTemp;
sTemp = "";
}
//Close the stream from the server
strm.Close();
result += "
";
}

//Finally, we catch any exceptions, and return our result stream of valid XML (or "exception", as the case may be):
catch (Exception)
{
//If exception occurred, provide user message
result = "exception";
}
//Return StockQuote(s) or Exception
return result;
}

}