请求聚合数据接口跨域问题

Posted on 2021-03-31 16:00  sesen  阅读(884)  评论(0编辑  收藏  举报

申请地址:https://www.juhe.cn/docs/api/id/21
接口地址:http://web.juhe.cn:8080/finance/stock/hs
返回格式:json
请求方式:get
请求示例:http://web.juhe.cn:8080/finance/stock/hs?gid=sh601009&key=您申请的APPKEY
接口备注:数据仅供参考,不作投资使用
请求参数说明:

名称 类型 必填 说明
gid string 股票编号,上海股市以sh开头,深圳股市以sz开头如:sh601009(type为0或者1时gid不是必须)
key string APP Key
type int 0代表上证指数,1代表深证指数

返回参数说明:

名称 类型 说明
见JSON返回示例 - -

JSON返回示例:

{
"resultcode":"200", /*返回码,200:正常*/
"reason":"SUCCESSED!",
"result":[
{
    "data":{
 		"gid":"sh601009",				/*股票编号*/
                "increPer": "9.91",                               /*涨跌百分比*/
                "increase": "43.99",                             /*涨跌额*/
		"name":"南京银行",				/*股票名称*/
		"todayStartPri":"8.26",				/*今日开盘价*/
		"yestodEndPri":"8.26",				/*昨日收盘价*/
		"nowPri":"8.37",				/*当前价格*/
		"todayMax":"8.55",				/*今日最高价*/
		"todayMin":"8.25",				/*今日最低价*/
		"competitivePri":"8.37",			/*竞买价*/
		"reservePri":"8.38",				/*竞卖价*/
		"traNumber":"34501453",				/*成交量*/
		"traAmount":"290889560",			/*成交金额*/
		"buyOne":"10870",				/*买一*/
		"buyOnePri":"8.37",				/*买一报价*/
		"buyTwo":"177241",				/*买二*/
		"buyTwoPri":"8.36",				/*买二报价*/
		"buyThree":"92600",				/*买三*/
		"buyThreePri":"8.35",				/*买三报价*/
		"buyFour":"87200"				/*买四*/
		"buyFourPri":"8.34",				/*买四报价*/
		"buyFive":"113700",				/*买五*/
		"buyFivePri":"8.42",				/*买五报价*/
		"sellOne":"47556",				/*卖一*/
		"sellOnePri":"8.38",				/*卖一报价*/
		"sellTwo":"103057",				/*卖二*/
		"sellTwoPri":"8.39",				/*卖二报价*/
		"sellThree":"186689",				/*卖三*/
		"sellThreePri":"8.40",				/*卖三报价*/
		"sellFour":"49000",				/*卖四*/
		"sellFourPri":"8.41",				/*卖四报价*/		
		"sellFive":"214535",				/*卖五*/
		"sellFivePri":"15.21",				/*卖五报价*/
		"date":"2012-12-11",				/*日期*/
		"time":"15:03:06",				/*时间*/
    },
    "dapandata":{/*大盘数据可通过字段type单独查询*/
    },
    "gopicture":{
		
	"minurl":"http://image.sinajs.cn/newchart/min/n/sh601009.gif",/*分时K线图*/
        "dayurl":"http://image.sinajs.cn/newchart/daily/n/sh601009.gif",/*日K线图*/
        "weekurl":"http://image.sinajs.cn/newchart/weekly/n/sh601009.gif",/*周K线图*/
        "monthurl":"http://image.sinajs.cn/newchart/monthly/n/sh601009.gif"/*月K线图*/
    }
}]
}
----------------------------------深(上)证指数示例---------------------------------------------------------
{
    "error_code": 0
    "reason": "SUCCESSED!",
    "result": {
        "dealNum": "24388041799",/*成交量*/
        "dealPri": "340674441059.270",/*成交额*/
        "highPri": "10357.417",/*最高*/
        "increPer": "-0.46",/*涨跌百分比*/
         "increase": "-43.756",/*涨跌幅*/
        "lowpri": "10121.741",/*最低*/
        "name": "深证成指",/*名称*/
        "nowpri": "10270.855",/*当前价格*/
        "openPri": "10200.547",/*今开*/
        "time": "2015-09-22 14:45:25",/*时间*/
        "yesPri": "10176.727"/*昨收*/
    },
}

ajax请求跨域,后来发现官方是在后台请求的。。。。。。。。。。。。。。。。所以大家一定要好好看官方文档

public ActionResult AjaxGuApi() {
 //1.沪深股市 
string url1 = "http://web.juhe.cn:8080/finance/stock/hs"; 
var parameters1 = new Dictionary
<string, string>(); 
parameters1.Add("gid" , "sz300128"); 
//股票编号,上海股市以sh开头,深圳股市以sz开头如:sh601009 parameters1.Add("key", "787eb248e055d5d5254f50c9a0b7e358");
//你申请的key string result1 = GuApi.sendPost(url1, parameters1, "get");
 return Json(result1); 
}

using 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 DTcms.Common
{
   public static class GuApi
    {
 
 
 
 
        //static void Main(string[] args)
        //{
        //    string appkey = "*******************"; //配置您申请的appkey
 
             
        //    //1.沪深股市
        //    string url1 = "http://web.juhe.cn:8080/finance/stock/hs";
 
        //    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.香港股市
        //    string url2 = "http://web.juhe.cn:8080/finance/stock/hk";
 
        //    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.美国股市
        //    string url3 = "http://web.juhe.cn:8080/finance/stock/usa";
 
        //    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.香港股市列表
        //    string url4 = "http://web.juhe.cn:8080/finance/stock/hkall";
 
        //    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.美国股市列表
        //    string url5 = "http://web.juhe.cn:8080/finance/stock/usaall";
 
        //    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.深圳股市列表
        //    string url6 = "http://web.juhe.cn:8080/finance/stock/szall";
 
        //    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.沪股列表
        //    string url7 = "http://web.juhe.cn:8080/finance/stock/shall";
 
        //    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>
       public 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();
            }
        }
 
    }
}

原文链接:https://blog.csdn.net/qq_36535245/article/details/78855719

Copyright © 2024 sesen
Powered by .NET 9.0 on Kubernetes