using System;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace LearningJustCode
{
class Program
{
static void Main(string[] args)
{
Update();
}
static void Update()
{
var quote1 = new { Stock = "DELL", Quote = GetQuote("DELL") };
var quote2 = new { Stock = "MSFT", Quote = GetQuote("MSFT") };
var quote3 = new { Stock = "GOOG", Quote = GetQuote("GOOG") };
var quotes = new object[] { quote1, quote2, quote3 };
foreach (var item in quotes)
{
Console.WriteLine(item.Stock);
Console.WriteLine(item.Quote.ToString());
}
Console.ReadKey();
}
static string GetQuote(string stock)
{
try
{
return InnerGetQuote(stock);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return "N/A";
}
}
static string InnerGetQuote(string stock)
{
string url = @"http://www.webservicex.net/stockquote.asmx/GetQuote?symbol={0}";
var request = HttpWebRequest.Create(string.Format(url, stock));
using (var response = request.GetResponse())
{
using (var reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
{
return reader.ReadToEnd();
}
}
}
}
}
我在项目上得到一个波浪线;变量“项目”仅分配给。此代码已根据 Paul Kimmel 的书 C# Unleashed by Sams 稍作修改。