当前位置:网站首页>Kinsoku Jikou Desu Sina stock interface change

Kinsoku Jikou Desu Sina stock interface change

2022-06-23 19:45:00 51CTO

1、 Question why

The Sina stock interface returns the value shown in the title :Kinsoku jikou desu!

 ​http://hq.sinajs.cn/list=code​​  The interface change of Sina stock , You need to add... To the request header Referer value .

Data source site : Sina finance and economics _ Sina.com

2、 Core code

      
      
/// <summary>
/// Get Sina stock cache
/// </summary>
/// <param name="args"> The code requesting the query </param>
/// <returns></returns>
[ HttpGet, HttpPost]
public string GetSinaStockCache( string args)
{
string url = "http://hq.sinajs.cn/list=" + args;
HttpWebRequest request = ( HttpWebRequest) WebRequest. Create( url); // Create a request example
// .NET Core edition
request. Headers. Add( "Referer", "https://finance.sina.com.cn/");
// .NET Framework edition
// request.Referer = "https://finance.sina.com.cn/";
HttpWebResponse response = ( HttpWebResponse) request. GetResponse(); // Get a response , Send the request
Stream responseStream = response. GetResponseStream();
StreamReader streamReader = new StreamReader( responseStream, Encoding. Default); // Default Chinese normal display ,UTF-8 Chinese can be garbled
string json = streamReader. ReadToEnd();
return json;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

To avoid interface changes , You can write to the cache when you get the response value , Give cache a valid period , As long as the returned information is abnormal, you can read the cache .

Finally, I would like to thank Sina stock interface for its constant support and great God's foresight .

3、 test

Request parameters (args):sz000651

Return results :

"var hq_str_sz000651=\" Gree electric appliances ,39.300,39.220,38.640,39.300,38.520,38.640,38.640,42165560,1634520898.300,465667,38.640,57333,0.000,0,0.000,0,0.000,0,0.000,465667,38.640,0,0.000,0,0.000,0,0.000,0,0.000,2022-02-17,14:59:06,00\";\n"


That's all Kinsoku jikou desu! Introduction to the changes of Sina stock interface , Make this record , If help , Welcome to like, pay attention to collection !

原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231833089394.html