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 StockQuoteWSRVC
///
[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 StockQuoteWSRVC : System.Web.Services.WebService {
public StockQuoteWSRVC () {
//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+="
sTemp+="
sTemp+="
sTemp+="";
sTemp+="
sTemp+="
sTemp+="
sTemp+="
sTemp+="
sTemp+="
sTemp+="
sTemp+="
sTemp+="
sTemp+="
sTemp+="
sTemp+="
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 ;
}
}