通过新浪网股票接口获取股票信息
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
通过新浪网股票接口获取股票信息
以下函数全是静态方法,可以在C#中直接调用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ;
using System.IO;
using System.Data;
using System.Text.RegularExpressions;
namespace GzyFunctionLibrary
{
public class GetStockInfo
{
public static string[] StockItems = new string[32] { "股票名字", "今日开盘价", "昨日收盘价", "当前价格", "今日最高价", "今日最低价", "竞买价,即买一报价", "竞卖价,即卖一报价", "成交的股票数", "成交金额", "买一申请多少股", "买一报价", "买二", "买二", "买三", "买三", "买四", "买四", "买五", "买五", "卖一申报股数", "卖一报价", "卖二申报股数", "卖二报价", "卖三申报股数", "卖三报价", "卖四申报股数", "卖四报价", "卖五申报股数", "卖五报价", "日期", "时间" };
public static string[] PastStockItems = new string[] { "指数名称", "当前点数", "涨跌点数", "涨跌率", "成交量(手)", "成交额(万元)" };
private static string url = "/list=";
private static string PastUrl = "/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;
else
return 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.Columns.Add("Opening", typeof(double));
dt.Columns.Add("MaxHight", typeof(double));
dt.Columns.Add("Closing", typeof(double));
dt.Columns.Add("MaxLow", typeof(double));
string url = string.Format("/corp/go.php/vMS_MarketHistory/stockid/{0}.p html", stockId);
.WebClient wc = new WebClient();
string Content = wc.DownloadString(url);