|
C#获取新浪实时股票数据:
参考链接:
http://blog.csdn.net/simon803/article/details/7784682
http://blog.csdn.net/shujudeliu/article/details/40818653
http://outofmemory.cn/code-snipp ... ter-chuan-come-file
UrlTools.cs:- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- namespace WindowsFormsApplication1
- {
- public class UrlTools
- {
- /*
- * 0:”大秦铁路”,股票名字;
- 1:”27.55″,今日开盘价;
- 2:”27.25″,昨日收盘价;
- 3:”26.91″,当前价格;
- 4:”27.55″,今日最高价;
- 5:”26.20″,今日最低价;
- 6:”26.91″,竞买价,即“买一”报价;
- 7:”26.92″,竞卖价,即“卖一”报价;
- 8:”22114263″,成交的股票数,由于股票交易以一百股为基本单位,所以在使用时,通常把该值除以一百;
- 9:”589824680″,成交金额,单位为“元”,为了一目了然,通常以“万元”为成交金额的单位,所以通常把该值除以一万;
- 10:”4695″,“买一”申请4695股,即47手;
- 11:”26.91″,“买一”报价;
- 12:”57590″,“买二”
- 13:”26.90″,“买二”
- 14:”14700″,“买三”
- 15:”26.89″,“买三”
- 16:”14300″,“买四”
- 17:”26.88″,“买四”
- 18:”15100″,“买五”
- 19:”26.87″,“买五”
- 20:”3100″,“卖一”申报3100股,即31手;
- 21:”26.92″,“卖一”报价
- (22, 23), (24, 25), (26,27), (28, 29)分别为“卖二”至“卖四的情况”
- 30:”2008-01-11″,日期;
- 31:”15:05:32″,时间;
- *
- *
- * **/
- /// <summary>
- /// 获取url的返回值
- /// </summary>
- /// <param name="url">eg:http://m.weather.com.cn/atad/101010100.html </param>
- public static string GetInfo(string url)
- {
- string strBuff = "";
- Uri httpURL = new Uri(url);
- ///HttpWebRequest类继承于WebRequest,并没有自己的构造函数,需通过WebRequest的Creat方法 建立,并进行强制的类型转换
- HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(httpURL);
- ///通过HttpWebRequest的GetResponse()方法建立HttpWebResponse,强制类型转换
- HttpWebResponse httpResp = (HttpWebResponse)httpReq.GetResponse();
- ///GetResponseStream()方法获取HTTP响应的数据流,并尝试取得URL中所指定的网页内容
- ///若成功取得网页的内容,则以System.IO.Stream形式返回,若失败则产生ProtoclViolationException错 误。在此正确的做法应将以下的代码放到一个try块中处理。这里简单处理
- Stream respStream = httpResp.GetResponseStream();
- ///返回的内容是Stream形式的,所以可以利用StreamReader类获取GetResponseStream的内容,并以
- //StreamReader类的Read方法依次读取网页源程序代码每一行的内容,直至行尾(读取的编码格式:UTF8)
- StreamReader respStreamReader = new StreamReader(respStream, Encoding.Default);
- strBuff = respStreamReader.ReadToEnd();
- return strBuff;
- }
- public static void WriteStringToFile(string fileName, string contents)
- {
- StreamWriter sWriter = null;
- try
- {
- FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
- sWriter = new StreamWriter(fileStream);
- sWriter.Write(contents);
- }
- finally
- {
- if (sWriter != null)
- {
- sWriter.Close();
- }
- }
- }
- public static void AppendStringToFile(string fileName, string contents)
- {
- StreamWriter sWriter = null;
- try
- {
- FileStream fileStream = new FileStream(fileName, FileMode.Append, FileAccess.Write);
- sWriter = new StreamWriter(fileStream);
- sWriter.Write(contents);
- }
- finally
- {
- if (sWriter != null)
- {
- sWriter.Close();
- }
- }
- }
- }
- }
复制代码
【获取当前行情】代码如下:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- private string baseUrlFormat = "http://hq.sinajs.cn/list={0}";
- private static string rowTitle = "股票名字 今日开盘价 昨日收盘价 当前价格 今日最高价 今日最低价 竞买价 竞卖价 成交的股票数 成交金额 买一 买二 买二 买三 买三 买四 买四 买五 买五 卖一 卖二 卖三 卖四 卖五 日期 时间" + Environment.NewLine;
- private string basePath = @"C:\Users\ysw\Desktop\New Folder99999999999999";
- public Form1()
- {
- InitializeComponent();
- }
- private void btnGetUrlData_Click(object sender, EventArgs e)
- {
- try
- {
- string stockCode = this.txtUrlName.Text;
- if (!string.IsNullOrEmpty(stockCode))
- {
- string fileNamePath = string.Format("{0}{1}.txt", basePath, stockCode);
- string url = string.Format(baseUrlFormat, stockCode);
- string date = UrlTools.GetInfo(url);
- int startIndex = date.IndexOf('"');
- int endIndex = date.LastIndexOf('"');
- string writeData = date.Substring(startIndex + 1, endIndex - startIndex - 1);
- string[] rowData = writeData.Split(',');
- writeData = writeData.Replace(",", "\t") + Environment.NewLine;
- //写入文件
- if (!File.Exists(fileNamePath))
- {
- UrlTools.WriteStringToFile(fileNamePath, rowTitle);
- this.richTextBox1.AppendText(rowTitle);
- }
- UrlTools.AppendStringToFile(fileNamePath, writeData);
- this.richTextBox1.AppendText(writeData);
- this.txtCurrentPrice.Text = rowData[3];
- MessageBox.Show("OK");
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- }
复制代码
C#源代码如下:链接:http://pan.baidu.com/s/1pLPnxzl 密码:mjgi
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|