调用webservice查看股票行情

直接上代码:  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 
 10 using System.Web;
 11 using System.Net;
 12 using System.IO;
 13 using System.Xml;
 14 
 15 namespace WindForm
 16 {
 17     public partial class Form1 : Form
 18     {
 19         public Form1()
 20         {
 21             InitializeComponent();
 22 
 23             //HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://stockhtm.finance.qq.com/sstock/ggcx/002182.shtml");
 24 
 25             //callService("sz002182");
 26             showdata(new string[]{"sz002182""sh600749"});
 27             timer1.Interval = 3000;
 28         }
 29 
 30         public void showdata(string[] str)
 31         {
 32             listBox1.Items.Clear();
 33             foreach (string x in str)
 34             {
 35                 callService(x);
 36                 listBox1.Items.Add("--  --  --");
 37                 listBox1.Items.Add("--  --  --");
 38                 listBox1.Items.Add("--  --  --");
 39             }
 40         }
 41         public void callService(string code)
 42         {
 43             HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://webservice.webxml.com.cn/WebServices/ChinaStockWebService.asmx/getStockInfoByCode?theStockCode="+code);
 44             request.MaximumResponseHeadersLength = 10240;
 45             request.Credentials = CredentialCache.DefaultCredentials;
 46             request.Method = "GET";
 47             request.Timeout = 60000;
 48             WebResponse webRep = request.GetResponse();
 49             Stream ReceiveStream = webRep.GetResponseStream();
 50             Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
 51 
 52             // Pipe the stream to a higher level stream reader with the required encoding format. 
 53             StreamReader readStream = new StreamReader(ReceiveStream, encode);
 54             string strstr = readStream.ReadToEnd();
 55             XmlDocument xml = new XmlDocument();
 56             xml.LoadXml(strstr);
 57 
 58             listBox1.Items.Add("Code:" + code + "-" + xml.ChildNodes[1].ChildNodes[1].InnerText);
 59             listBox1.Items.Add("sell5:" + xml.ChildNodes[1].ChildNodes[15].InnerText.Split('/')[0+ "块," + xml.ChildNodes[1].ChildNodes[15].InnerText.Split('/')[1+ "");
 60             listBox1.Items.Add("sell4:" + xml.ChildNodes[1].ChildNodes[16].InnerText.Split('/')[0+ "块," + xml.ChildNodes[1].ChildNodes[16].InnerText.Split('/')[1+ "");
 61             listBox1.Items.Add("sell3:" + xml.ChildNodes[1].ChildNodes[17].InnerText.Split('/')[0+ "块," + xml.ChildNodes[1].ChildNodes[17].InnerText.Split('/')[1+ "");
 62             listBox1.Items.Add("sell2:" + xml.ChildNodes[1].ChildNodes[18].InnerText.Split('/')[0+ "块," + xml.ChildNodes[1].ChildNodes[18].InnerText.Split('/')[1+ "");
 63             listBox1.Items.Add("sell1:" + xml.ChildNodes[1].ChildNodes[19].InnerText.Split('/')[0+ "块," + xml.ChildNodes[1].ChildNodes[19].InnerText.Split('/')[1+ "");
 64             listBox1.Items.Add("--  --  --");
 65             listBox1.Items.Add("now:" + xml.ChildNodes[1].ChildNodes[3].InnerText);
 66             listBox1.Items.Add("--  --  --");
 67             listBox1.Items.Add("buy1:" + xml.ChildNodes[1].ChildNodes[20].InnerText.Split('/')[0+ "块," + xml.ChildNodes[1].ChildNodes[20].InnerText.Split('/')[1+ "");
 68             listBox1.Items.Add("buy2:" + xml.ChildNodes[1].ChildNodes[21].InnerText.Split('/')[0+ "块," + xml.ChildNodes[1].ChildNodes[21].InnerText.Split('/')[1+ "");
 69             listBox1.Items.Add("buy3:" + xml.ChildNodes[1].ChildNodes[22].InnerText.Split('/')[0+ "块," + xml.ChildNodes[1].ChildNodes[22].InnerText.Split('/')[1+ "");
 70             listBox1.Items.Add("buy4:" + xml.ChildNodes[1].ChildNodes[23].InnerText.Split('/')[0+ "块," + xml.ChildNodes[1].ChildNodes[23].InnerText.Split('/')[1+ "");
 71             listBox1.Items.Add("buy5:" + xml.ChildNodes[1].ChildNodes[24].InnerText.Split('/')[0+ "块," + xml.ChildNodes[1].ChildNodes[24].InnerText.Split('/')[1+ "");
 72 
 73             //XmlNodeList xnl = xml.SelectNodes("ArrayOfString/string");
 74             //for (int i = 0; i < xnl.Count; i++)
 75             //{
 76             //    if (i == 19)
 77             //        listBox1.Items.Add("sell:" + xnl[i].Value.Split('/')[0] + "" + xnl[i].Value.Split('/')[1]);
 78             //    if (i == 20)
 79             //        listBox1.Items.Add("buy:" + xnl[i].Value.Split('/')[0] + "" + xnl[i].Value.Split('/')[1]);
 80             //}
 81 
 82             //Char[] read = new Char[256];
 83 
 84             //// Read 256 charcters at a time.    
 85             //int count = readStream.Read(read, 0, 256);
 86 
 87             //while (count > 0)
 88             //{
 89             //    // Dump the 256 characters on a string and display the string onto the console.
 90             //    String str = new String(read, 0, count);
 91             //    count = readStream.Read(read, 0, 256);
 92             //}
 93 
 94             // Release the resources of stream object.
 95             readStream.Close();
 96 
 97             webRep.Close();
 98         }
 99 
100         private void timer1_Tick(object sender, EventArgs e)
101         {
102             showdata(new string[] { "sz002182""sh600749" });
103         }
104     }
105 }
106 //http://webservice.webxml.com.cn/WebServices/StockInfoWS.asmx?wsdl
107 /*
108  输入参数:theStockCode = 股票代号,如:sh000001; 返回数据:一个一维字符串数组 String(24),
109  * 结构为:
110  * String(0)股票代号、String(1)股票名称、String(2)行情时间、String(3)最新价(元)、String(4)昨收盘(元)、
111  * String(5)今开盘(元)、String(6)涨跌额(元)、String(7)最低(元)、String(8)最高(元)、String(9)涨跌幅(%)、
112  * String(10)成交量(手)、String(11)成交额(万元)、String(12)竞买价(元)、String(13)竞卖价(元)、String(14)委比(%)、
113  * String(15)-String(19)买一 - 买五(元)/手、String(20)-String(24)卖一 - 卖五(元)/手。
114  *
原文地址:https://www.cnblogs.com/bober/p/1968967.html