小博股数据开放平台是面向全部用户的股票数据开放平台,通过调用接口可以获取股票的历史数据。在调用之前需要进行下面的准备工作,第一步注册:
用户在注册之后,登录点击头像进入个人中心,在功能模块的最下方有一个创建应用
点击创建应用的按钮,我们可以输入应用的名称和用途,token有效期如果想简单一点可以选择长期有效
点击添加就可以了,创建了应用之后,我们可以这里有appkey和appsecret,用来获取token的了
当然如果你选择了永久有效,其实页面上的token直接就可以拿来用了,就不用下面的调用接口来获了,
如果你选择的token有效期是24小时有效,那么我们看接下来如何获取token
public class GetToken {
private final static String appkey="*****";
private final static String appsecret="*****";
private final static String PostUrl="https://www.read8686.com/plus/openapi/gettoken";
public static void main(String[] args) {
JSONObject requestParams=new JSONObject();
requestParams.put("appkey", appkey);
requestParams.put("appsecret",appsecret);
String params=requestParams.toString();
System.out.println(params);
String result=HttpsUtil.doPost(PostUrl,params,"utf-8");
System.out.println(result);
}
}
代码执行的返回结果:
{
"msg":"获取成功",
"code":"0000",
"expired":"2024-04-06 09:27:38",
"token":"5cf947e1-9e5d-46c1-bc37-61e98b5555b0"
}
这里我们用到的两个工具类,来看一下:HttpsUtil:
package com.sdk.util;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
/**
* 利用HttpClient进行post请求的工具类
* @ClassName: HttpClientUtil
* @Description: TODO
* @author Devin <xxx>
* @date 2017年2月7日 下午1:43:38
*
*/
public class HttpsUtil {
@SuppressWarnings("resource")
public static String doPost(String url,String jsonstr,String charset){
HttpClient httpClient = null;
HttpPost httpPost = null;
String result = null;
try{
httpClient = new SSLClient();
httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json");
StringEntity se = new StringEntity(jsonstr);
se.setContentType("text/json");
se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
httpPost.setEntity(se);
HttpResponse response = httpClient.execute(httpPost);
if(response != null){
HttpEntity resEntity = response.getEntity();
if(resEntity != null){
result = EntityUtils.toString(resEntity,charset);
}
}
}catch(Exception ex){
ex.printStackTrace();
}
return result;
}
}
SSLClient:
package com.sdk.util;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
/**
* 用于进行Https请求的HttpClient
* @ClassName: SSLClient
* @Description: TODO
* @author Devin <xxx>
* @date 2017年2月7日 下午1:42:07
*
*/
public class SSLClient extends DefaultHttpClient {
public SSLClient() throws Exception{
super();
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = this.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", 443, ssf));
}
}
希望对你有所帮助