我在编码方面还很陌生,希望能得到解决问题的建议。我正在编写一个 java 代码来连接到 trade.io API,但我不知道如何使用“POST”和“DELETE”向交易所提交正确的加密消息。
到目前为止,我设法弄清楚了如何使用“GET”接收信息,但在其他方面没有成功。
这是我到目前为止写的:
/*
* CancelOrder cancels an existing order ==> This doesn't work!
*/
public String CancelOrder(String orderId) throws MalformedURLException,
IOException {
return signAndSend("/order/" + orderId, "DELETE");
}
/*
* Reads the open orders in the account ==> This works!
*/
public String getOpenOrders(String symbol) throws MalformedURLException,
IOException {
return signAndSend("/openOrders/" + symbol, "GET");
}
/*
* Signs and Sends signature. This method is called when signature is
needed.
*/
private String signAndSend(String url, String method) throws
MalformedURLException, IOException {
String nonce = String.valueOf(System.currentTimeMillis());
String baseUrl = UrlTradeio.urlV1;
String ts = "?ts=" + nonce;
String sign = hmac512Digest(ts, TRADEIO_SECRET_KEY).toUpperCase();
HttpURLConnection con = (HttpURLConnection) new URL(baseUrl + url + ts).openConnection();
con.setRequestProperty("Sign", sign);
con.setRequestProperty("Key", TRADEIO_API_KEY);
con.setRequestMethod(method);
con.connect();
InputStream response = con.getInputStream();
//
try (Scanner scanner = new Scanner(response)) {
String responseBody = scanner.next();
return responseBody;
}
}
交易所在此处提供了非常详尽的 C# 示例:
https://github.com/tradeio/api-csharpclient/blob/master/Tradeio.Client/TradeioApi.cs
这是“getOpenOrders”的输出和我尝试关闭它时的错误消息。
线程“main”中的异常 java.io.IOException:服务器返回 HTTP 响应代码:403 URL:https ://api.exchange.trade.io/api/v1/order/-72057593948251267?ts= 1559338452695 at java.base /sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1913)
翻阅古今
相关分类