System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Net;
using
System.IO;
using
Xfrog.Net;
using
System.Diagnostics;
using
System.Web;
//----------------------------------
// 股票數據調用示例代碼 - 聚合數據
// 在線接口文檔:http://www.juhe.cn/docs/21
// 代碼中JsonObject類下載地址:http://download.csdn.net/download/gcm3206021155665/7458439
//----------------------------------
namespace
ConsoleAPI
{
class
Program
{
static
void
Main(
string
[] args)
{
string
appkey =
"*******************"
;
//配置您申請的appkey
//1.滬深股市
var parameters1 =
new
Dictionary<
string
,
string
>();
parameters1.Add(
"gid"
,
""
);
//股票編號,上海股市以sh開頭,深圳股市以sz開頭如:sh601009
parameters1.Add(
"key"
, appkey);
//你申請的key
string
result1 = sendPost(url1, parameters1,
"get"
);
JsonObject newObj1 =
new
JsonObject(result1);
String errorCode1 = newObj1[
"error_code"
].Value;
if
(errorCode1 ==
"0"
)
{
Debug.WriteLine(
"成功"
);
Debug.WriteLine(newObj1);
}
else
{
//Debug.WriteLine("失敗");
Debug.WriteLine(newObj1[
"error_code"
].Value+
":"
+newObj1[
"reason"
].Value);
}
//2.香港股市
var parameters2 =
new
Dictionary<
string
,
string
>();
parameters2.Add(
"num"
,
""
);
//股票代碼,如:00001 為“長江實業”股票代碼
parameters2.Add(
"key"
, appkey);
//你申請的key
string
result2 = sendPost(url2, parameters2,
"get"
);
JsonObject newObj2 =
new
JsonObject(result2);
String errorCode2 = newObj2[
"error_code"
].Value;
if
(errorCode2 ==
"0"
)
{
Debug.WriteLine(
"成功"
);
Debug.WriteLine(newObj2);
}
else
{
//Debug.WriteLine("失敗");
Debug.WriteLine(newObj2[
"error_code"
].Value+
":"
+newObj2[
"reason"
].Value);
}
//3.美國股市
var parameters3 =
new
Dictionary<
string
,
string
>();
parameters3.Add(
"gid"
,
""
);
//股票代碼,如:aapl 為“蘋果公司”的股票代碼
parameters3.Add(
"key"
, appkey);
//你申請的key
string
result3 = sendPost(url3, parameters3,
"get"
);
JsonObject newObj3 =
new
JsonObject(result3);
String errorCode3 = newObj3[
"error_code"
].Value;
if
(errorCode3 ==
"0"
)
{
Debug.WriteLine(
"成功"
);
Debug.WriteLine(newObj3);
}
else
{
//Debug.WriteLine("失敗");
Debug.WriteLine(newObj3[
"error_code"
].Value+
":"
+newObj3[
"reason"
].Value);
}
//4.香港股市列表
var parameters4 =
new
Dictionary<
string
,
string
>();
parameters4.Add(
"key"
, appkey);
//你申請的key
parameters4.Add(
"page"
,
""
);
//第幾頁,每頁20條數據,默認第1頁
string
result4 = sendPost(url4, parameters4,
"get"
);
JsonObject newObj4 =
new
JsonObject(result4);
String errorCode4 = newObj4[
"error_code"
].Value;
if
(errorCode4 ==
"0"
)
{
Debug.WriteLine(
"成功"
);
Debug.WriteLine(newObj4);
}
else
{
//Debug.WriteLine("失敗");
Debug.WriteLine(newObj4[
"error_code"
].Value+
":"
+newObj4[
"reason"
].Value);
}
//5.美國股市列表
var parameters5 =
new
Dictionary<
string
,
string
>();
parameters5.Add(
"key"
, appkey);
//你申請的key
parameters5.Add(
"page"
,
""
);
//第幾頁,每頁20條數據,默認第1頁
string
result5 = sendPost(url5, parameters5,
"get"
);
JsonObject newObj5 =
new
JsonObject(result5);
String errorCode5 = newObj5[
"error_code"
].Value;
if
(errorCode5 ==
"0"
)
{
Debug.WriteLine(
"成功"
);
Debug.WriteLine(newObj5);
}
else
{
//Debug.WriteLine("失敗");
Debug.WriteLine(newObj5[
"error_code"
].Value+
":"
+newObj5[
"reason"
].Value);
}
//6.深圳股市列表
var parameters6 =
new
Dictionary<
string
,
string
>();
parameters6.Add(
"key"
, appkey);
//你申請的key
parameters6.Add(
"page"
,
""
);
//第幾頁(每頁20條數據),默認第1頁
string
result6 = sendPost(url6, parameters6,
"get"
);
JsonObject newObj6 =
new
JsonObject(result6);
String errorCode6 = newObj6[
"error_code"
].Value;
if
(errorCode6 ==
"0"
)
{
Debug.WriteLine(
"成功"
);
Debug.WriteLine(newObj6);
}
else
{
//Debug.WriteLine("失敗");
Debug.WriteLine(newObj6[
"error_code"
].Value+
":"
+newObj6[
"reason"
].Value);
}
//7.滬股列表
var parameters7 =
new
Dictionary<
string
,
string
>();
parameters7.Add(
"key"
, appkey);
//你申請的key
parameters7.Add(
"page"
,
""
);
//第幾頁,每頁20條數據,默認第1頁
string
result7 = sendPost(url7, parameters7,
"get"
);
JsonObject newObj7 =
new
JsonObject(result7);
String errorCode7 = newObj7[
"error_code"
].Value;
if
(errorCode7 ==
"0"
)
{
Debug.WriteLine(
"成功"
);
Debug.WriteLine(newObj7);
}
else
{
//Debug.WriteLine("失敗");
Debug.WriteLine(newObj7[
"error_code"
].Value+
":"
+newObj7[
"reason"
].Value);
}
}
/// <summary>
/// Http (GET/POST)
/// </summary>
/// <param name="url">請求URL</param>
/// <param name="parameters">請求參數</param>
/// <param name="method">請求方法</param>
/// <returns>響應內容</returns>
static
string
sendPost(
string
url, IDictionary<
string
,
string
> parameters,
string
method)
{
if
(method.ToLower() ==
"post"
)
{
HttpWebRequest req =
null
;
HttpWebResponse rsp =
null
;
System.IO.Stream reqStream =
null
;
try
{
req = (HttpWebRequest)WebRequest.Create(url);
req.Method = method;
req.KeepAlive =
false
;
req.ProtocolVersion = HttpVersion.Version10;
req.Timeout = 5000;
req.ContentType =
"application/x-www-form-urlencoded;charset=utf-8"
;
byte
[] postData = Encoding.UTF8.GetBytes(BuildQuery(parameters,
"utf8"
));
reqStream = req.GetRequestStream();
reqStream.Write(postData, 0, postData.Length);
rsp = (HttpWebResponse)req.GetResponse();
Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
return
GetResponseAsString(rsp, encoding);
}
catch
(Exception ex)
{
return
ex.Message;
}
finally
{
if
(reqStream !=
null
) reqStream.Close();
if
(rsp !=
null
) rsp.Close();
}
}
else
{
//創建請求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url +
"?"
+ BuildQuery(parameters,
"utf8"
));
//GET請求
request.Method =
"GET"
;
request.ReadWriteTimeout = 5000;
request.ContentType =
"text/html;charset=UTF-8"
;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader =
new
StreamReader(myResponseStream, Encoding.GetEncoding(
"utf-8"
));
//返回內容
string
retString = myStreamReader.ReadToEnd();
return
retString;
}
}
/// <summary>
/// 組裝普通文本請求參數。
/// </summary>
/// <param name="parameters">Key-Value形式請求參數字典</param>
/// <returns>URL編碼后的請求數據</returns>
static
string
BuildQuery(IDictionary<
string
,
string
> parameters,
string
encode)
{
StringBuilder postData =
new
StringBuilder();
bool
hasParam =
false
;
IEnumerator<KeyValuePair<
string
,
string
>> dem = parameters.GetEnumerator();
while
(dem.MoveNext())
{
string
name = dem.Current.Key;
string
value = dem.Current.Value;
// 忽略參數名或參數值為空的參數
if
(!
string
.IsNullOrEmpty(name))
//&& !string.IsNullOrEmpty(value)
{
if
(hasParam)
{
postData.Append(
"&"
);
}
postData.Append(name);
postData.Append(
"="
);
if
(encode ==
"gb2312"
)
{
postData.Append(HttpUtility.UrlEncode(value, Encoding.GetEncoding(
"gb2312"
)));
}
else
if
(encode ==
"utf8"
)
{
postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));
}
else
{
postData.Append(value);
}
hasParam =
true
;
}
}
return
postData.ToString();
}
/// <summary>
/// 把響應流轉換為文本。
/// </summary>
/// <param name="rsp">響應流對象</param>
/// <param name="encoding">編碼方式</param>
/// <returns>響應文本</returns>
static
string
GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
{
System.IO.Stream stream =
null
;
StreamReader reader =
null
;
try
{
// 以字符流的方式讀取HTTP響應
stream = rsp.GetResponseStream();
reader =
new
StreamReader(stream, encoding);
return
reader.ReadToEnd();
}
finally
{
// 釋放資源
if
(reader !=
null
) reader.Close();
if
(stream !=
null
) stream.Close();
if
(rsp !=
null
) rsp.Close();
}
}
}
}
測試