获取中国银行网页中外汇率

来源:互联网 发布:gta捏脸数据女生 编辑:程序博客网 时间:2024/04/28 13:36
using System;using System.Collections.Generic;using System.Text;//============================using System.Net;using System.IO;namespace BOSS.网关程序.日常工作{    public class synchronousRate    {                   public Dictionary<string, double> getRate() {            //字典----------------------------------------------------------------------------            Dictionary<string, double> huobi = new Dictionary<string, double>();            //--------------------------------------------------------------------------------            string strHtml = GetWebContent("http://www.boc.cn/sourcedb/whpj/enindex.html").Trim();            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();            doc.LoadHtml(strHtml);            doc.OptionOutputAsXml = true;            HtmlAgilityPack.HtmlNode node = doc.DocumentNode.SelectSingleNode(".//table[tr/th=\"Currency Name\"]");            //if (node == null)            //{            //    Show("没有找到有th为Currency Name列的table", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information);            //    return;            //}            HtmlAgilityPack.HtmlNodeCollection trNodeList = node.SelectNodes("tr[td]");            foreach (HtmlAgilityPack.HtmlNode trNode in trNodeList)            {                //DataRow dr = dt.NewRow();                //dt.Rows.Add(dr);                //for (int j = 0; j < 7; j++)                 //{                //    HtmlAgilityPack.HtmlNodeCollection tdNodeList = trNode.SelectNodes("td");                //    dr[j] = tdNodeList[j].InnerText.Replace(" ", " ");                 //}                HtmlAgilityPack.HtmlNodeCollection tdNodeList1 = trNode.SelectNodes("td");                switch (tdNodeList1[0].InnerText.Replace(" ", " "))                {                    case "AUD":                    case "EUR":                    case "GBP":                    case "USD":                        huobi.Add(tdNodeList1[0].InnerText.Replace(" ", " "), double.Parse(tdNodeList1[4].InnerText) / 100); break;                }            }            return huobi;        }        private string GetWebContent(string strUrl)        {            string str = "";            try            {                WebClient wc = new WebClient();                wc.Credentials = CredentialCache.DefaultCredentials;                Encoding enc = Encoding.GetEncoding("UTF-8");// 如果是乱码就改成 UTF-8 / GB2312                            //方法一:                //Byte[] buffer = wc.DownloadData(strUrl);// 从资源下载数据并返回字节数组                //str = enc.GetString(buffer);// 输出字符串(HTML代码)                //方法二:                Stream res = wc.OpenRead(strUrl);//以流的形式打开URL                //Stream res = new FileStream(@"E:\ExchangeRate\enindex.html", FileMode.Open, FileAccess.Read);                StreamReader sr = new StreamReader(res, enc);//以指定的编码方式读取数据流                str = sr.ReadToEnd();//输出(HTML代码)                res.Close();                wc.Dispose();            }            catch (Exception ex)            {                           }            return str;        }    }}

0 0