Skip to content

Commit

Permalink
java demo
Browse files Browse the repository at this point in the history
  • Loading branch information
buyongji committed Oct 21, 2019
1 parent 86700c5 commit f72cf78
Show file tree
Hide file tree
Showing 17 changed files with 551 additions and 0 deletions.
12 changes: 12 additions & 0 deletions java/coinex_demo_java.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="lib" level="project" />
</component>
</module>
Binary file added java/lib/commons-codec-1.7.jar
Binary file not shown.
Binary file added java/lib/commons-io-2.2.jar
Binary file not shown.
Binary file added java/lib/commons-logging-1.0.4.jar
Binary file not shown.
Binary file added java/lib/fastjson-1.1.23.jar
Binary file not shown.
Binary file added java/lib/httpclient-4.3.6.jar
Binary file not shown.
Binary file added java/lib/httpcore-4.3.3.jar
Binary file not shown.
Binary file added java/lib/jackson-annotations-2.0.2.jar
Binary file not shown.
Binary file added java/lib/jackson-core-2.0.2.jar
Binary file not shown.
Binary file added java/lib/jackson-databind-2.0.2.jar
Binary file not shown.
11 changes: 11 additions & 0 deletions java/src/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>src</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>
27 changes: 27 additions & 0 deletions java/src/com/CoinEx/stock/rest/CoinExHttpDelete.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.CoinEx.stock.rest;

import java.net.URI;

import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;

public class CoinExHttpDelete extends HttpEntityEnclosingRequestBase {
public static final String METHOD_NAME = "DELETE";

public String getMethod() {
return METHOD_NAME;
}

public CoinExHttpDelete(final String uri) {
super();
setURI(URI.create(uri));
}

public CoinExHttpDelete(final URI uri) {
super();
setURI(uri);
}

public CoinExHttpDelete() {
super();
}
}
23 changes: 23 additions & 0 deletions java/src/com/CoinEx/stock/rest/CoinExTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.CoinEx.stock.rest;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.http.HttpException;

public class CoinExTest {

public static void main(String[] args) throws HttpException, IOException{

String access_id = "9578C0CF6F6847C6A6E7D22128FC6BDA"; //CoinEx申请的apiKey
String secret_key = "65DC683285FD400FAE8F81860094D69012C3C8EFA0792D7A"; //CoinEx 申请的secret_key
// String url_prex = "https://api.coinex.com/v1/";
String url_prex = "https://testapi.coinex.com/v1/";

StockApi api = new StockApi(url_prex, access_id, secret_key);
String account = api.account();
Logger.getGlobal().log(Level.INFO, account);

}
}
173 changes: 173 additions & 0 deletions java/src/com/CoinEx/stock/rest/HttpUtilManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package com.CoinEx.stock.rest;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.protocol.HttpContext;

import com.alibaba.fastjson.JSON;



/**
* 封装HTTP请求
* @author xiaoxibo
*
*/
public class HttpUtilManager {

private static HttpUtilManager instance = new HttpUtilManager();
private static HttpClient client;
private static long startTime = System.currentTimeMillis();
public static PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
private static ConnectionKeepAliveStrategy keepAliveStrat = new DefaultConnectionKeepAliveStrategy() {

public long getKeepAliveDuration(
HttpResponse response,
HttpContext context) {
long keepAlive = super.getKeepAliveDuration(response, context);

if (keepAlive == -1) {
keepAlive = 5000;
}
return keepAlive;
}

};
private HttpUtilManager() {
client = HttpClients.custom().setConnectionManager(cm).setKeepAliveStrategy(keepAliveStrat).build();
}

private static RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(20000)
.setConnectTimeout(20000)
.setConnectionRequestTimeout(20000)
.build();


public static HttpUtilManager getInstance() {
return instance;
}

public HttpClient getHttpClient() {
return client;
}

private HttpPost httpPostMethod(String url, String authorization) {
HttpPost method = new HttpPost(url);
method.setHeader("Content-Type", "application/json");
method.setHeader("authorization", authorization);
return method;
}

private CoinExHttpDelete httpDeleteMethod(String url, String authorization) {
CoinExHttpDelete method = new CoinExHttpDelete(url);
method.setHeader("Content-Type", "application/json");
method.setHeader("authorization", authorization);
return method;
}

private HttpRequestBase httpGetMethod(String url, String authorization) {
HttpGet method = new HttpGet(url);
method.setHeader("Content-Type", "application/json");
method.setHeader("authorization", authorization);
return method;
}

public String requestHttpGet(String url_prex, String url, Map<String, String> paramMap, String authorization) throws HttpException, IOException{

url=url_prex+url + "?" + StringUtil.createLinkString(paramMap);
if (paramMap == null) {
paramMap = new HashMap<>();
}

HttpRequestBase method = this.httpGetMethod(url, authorization);
method.setConfig(requestConfig);
HttpResponse response = client.execute(method);
HttpEntity entity = response.getEntity();
if(entity == null){
return "";
}
InputStream is = null;
String responseData = "";
try{
is = entity.getContent();
responseData = IOUtils.toString(is, "UTF-8");
}finally{
if(is!=null){
is.close();
}
}
return responseData;
}

public String requestHttpPost(String url_prex,String url,Map<String,String> params, String authorization) throws HttpException, IOException{

url=url_prex+url;
HttpPost method = this.httpPostMethod(url, authorization);
StringEntity sendEntity = new StringEntity(JSON.toJSONString(params));
method.setEntity(sendEntity);
method.setConfig(requestConfig);
HttpResponse response = client.execute(method);
HttpEntity entity = response.getEntity();
if(entity == null){
return "";
}
InputStream is = null;
String responseData = "";
try{
is = entity.getContent();
responseData = IOUtils.toString(is, "UTF-8");
}finally{
if(is!=null){
is.close();
}
}
return responseData;
}

public String requestHttpDelete(String url_prex,String url,Map<String,String> params, String authorization) throws HttpException, IOException{

url=url_prex+url;
CoinExHttpDelete method = this.httpDeleteMethod(url, authorization);
StringEntity sendEntity = new StringEntity(JSON.toJSONString(params));
method.setEntity(sendEntity);
method.setConfig(requestConfig);
HttpResponse response = client.execute(method);
HttpEntity entity = response.getEntity();
if(entity == null){
return "";
}
InputStream is = null;
String responseData = "";
try{
is = entity.getContent();
responseData = IOUtils.toString(is, "UTF-8");
}finally{
if(is!=null){
is.close();
}
}
return responseData;
}
}

58 changes: 58 additions & 0 deletions java/src/com/CoinEx/stock/rest/MD5Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.CoinEx.stock.rest;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class MD5Util {

/**
* 生成签名结果
*
* @param sArray
* 要签名的数组
* @return 签名结果字符串
*/
public static String buildMysignV1(Map<String, String> sArray,
String secretKey) {
String mysign = "";
try {
String prestr = StringUtil.createLinkString(sArray); // 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
prestr = prestr + "&secret_key=" + secretKey; // 把拼接后的字符串再与安全校验码连接起来
mysign = getMD5String(prestr);
} catch (Exception e) {
e.printStackTrace();
}
return mysign;
}

/**
* 生成32位大写MD5值
*/
private static final char HEX_DIGITS[] = { '0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

public static String getMD5String(String str) {
try {
if (str == null || str.trim().length() == 0) {
return "";
}
byte[] bytes = str.getBytes();
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(bytes);
bytes = messageDigest.digest();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
sb.append(HEX_DIGITS[(bytes[i] & 0xf0) >> 4] + ""
+ HEX_DIGITS[bytes[i] & 0xf]);
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
}
Loading

0 comments on commit f72cf78

Please sign in to comment.