资源预览内容
第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
亲,该文档总共4页全部预览完了,如果喜欢就下载吧!
资源描述
通过新浪网股票接口获取股票信息以下函数全是静态方法,可以在C#中直接调用using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.IO;using System.Data;using System.Text.RegularExpressions;namespace GzyFunctionLibrarypublic class GetStockInfopublic static string StockItems = new string32 股票名字,今日开盘价,昨日收 盘价,当前价格,今日最高价,今日最低价,竞买价,即买一报价,竞卖价,即卖一报 价,成交的股票数,成交金额,买一申请多少股,买一报价,买二,买二,买三,买 三,买四,买四,买五,买五,卖一申报股数,卖一报价,卖二申报股数,卖二报价 ,卖三申报股数,卖三报价,卖四申报股数,卖四报价,卖五申报股数,卖五报价, 日期,时间;public static string PastStockItems = new string 指数名称,当前点数,涨跌点 数,涨跌率,成交量(手),成交额(万元);private static string url = http:/hq.sinajs.cn/list=;private static string PastUrl = http:/hq.sinajs.cn/list=s_;/ /生成/ / / / private static string GetUrl(string StockID, int ValueType)string id = string.Empty;if (StockID = | StockID.Length != 6)return null;string dm = sh;if (StockID.Trim() = 000001)dm = sh; else if (StockID.Trim() = 399001) dm = sz;else if (StockID.Substring(0, 2) = 00)/深圳 dm = sz;else if (StockID.Substring(0, 2) = 60) dm = sh;else if (StockID.Substring(0, 2) = 51)/上海基金 dm = sh;switch (ValueType)case 0:return url + dm + StockID;case 1:return PastUrl + dm + StockID;default:return url + dm + StockID;/ /获取服务实时及历史信息/ / / public static string GetValue(string url) try string StockValue;WebClient myWebClient = new WebClient();Stream myStream = myWebClient.OpenRead(url);StreamReader sr = new StreamReader(myStream, System.Text.Encoding.GetEncoding(gbk); string strHTML = sr.ReadToEnd();myStream.Close();sr.Close();int first = strHTML.IndexOf();strHTML = strHTML.Substring(first + 1, strHTML.Length - first - 4);StockValue = strHTML.Split(,); if (StockValue.Length = 2) return StockValue;elsereturn null;catch (Exception ex)return null;throw ex;/ /获取股票实时信息/ / / public static string GetRealValue(string StockId)return GetValue(GetUrl(StockId, 0);/ /获取股票历史信息/ / / public static string GetPastValue(string StockId)return GetValue(GetUrl(StockId, 1);public static DataTable getHis(string stockId)DataTable dt = new DataTable();dt.Columns.Add(StockId, typeof(string);dt.Columns.Add(iDate, typeof(DateTime);dt.Co山mns.Add(Opening, typeof(double);dt.Co山mns.Add(MaxHight, typeof(double);dt.Co山mns.Add(Closing, typeof(double);dt.Columns.Add(MaxLow, typeof(double);stringurl=string.Format(http:/money.finance.sina.com.cn/corp/go.php/vMS_MarketHistory/stockid/0.p html, stockId);System.Net.WebClient wc = new WebClient();string Content = wc.DownloadString(url);stringreg=s*(As+)s+s*s*tdAd*(A*)s+tdAd *(A*)s+tdAd*(A*)s+tdAd*(A*)s+;MatchCollection result = Regex.Matches(Content, reg);foreach (Match item in result)DataRow dr = dt.NewRow();dr0 = stockId;dr1 = item.Groups1.Value;/时间dr2 = item.Groups2.Value;/时间dr3 = item.Groups3.Value;/时间dr=item.Groups 4.Value;/时间dr5 = item.Groups5.Value;/时间return dt;
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号