我已经看到了多个线程来解决此问题,但是不幸的是,我无法弄清楚如何从中解决问题。 您可能需要我的编码技能仍然有限,因此需要帮助来找出如何修复Java代码。
设法找到“ GET”和“ DELETE”方法的解决方案。 有一个用C#和CURL编写的示例,但是我的技能仅限于解释部分代码。
https://github.com/tradeio/api-csharpclient/blob/master/Tradeio.Client/TradeioApi.cs
这是我的Java实现:
public String PlaceOrder(String symbol, String side, String type, String quantity, String price) throws MalformedURLException, IOException {
LinkedHashMap<String, String> form = new LinkedHashMap<String, String>();
form.put("Symbol", symbol);
form.put("Side", side);
form.put("Type", type);
form.put("Quantity", quantity);
form.put("Price", price);
form.put("ts", String.valueOf(System.currentTimeMillis()));
return signAndSend2("/order", form, "POST");
}
private String signAndSend2(String url, LinkedHashMap<String, String> payload, String method)
throws MalformedURLException, IOException {
String nonce = String.valueOf(System.currentTimeMillis());
String baseUrl = UrlTradeio.urlV1;
String sign = "";
HttpURLConnection conn = null;
JSONObject json = new JSONObject(payload);
String formForPayload = json + "";
StringBuilder sb = new StringBuilder();
for (Entry<String, String> param : payload.entrySet()) {
if (sb.length() != 0)
sb.append('&');
sb.append(URLEncoder.encode(param.getKey(), "UTF-8"));
sb.append('=');
sb.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = sb.toString().getBytes("UTF-8");
int postDataLength = postDataBytes.length;
// System.out.println(sb);
// System.out.println(formForPayload);
sign = hmac512Digest(formForPayload, TRADEIO_SECRET_KEY).toUpperCase();
conn = (HttpURLConnection) new URL(baseUrl + url).openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("Sign", sign);
conn.setRequestProperty("Key", TRADEIO_API_KEY);
conn.setRequestProperty("ts", nonce);
conn.setRequestProperty("accept", "application/json");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Content-Length", Integer.toString(postDataLength));
conn.setRequestMethod(method);
conn.getOutputStream().write(postDataBytes);
conn.connect();
Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
StringBuilder output = new StringBuilder();
for (int c; (c = in.read()) >= 0;)
sb.append((char)c);
String responseBody = output.toString();
return responseBody;
}
根据我的尝试,我会收到一些错误消息。
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 500 for URL: https://api.exchange.trade.io/api/v1/order
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1913)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1509)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:245)
at Private.TradeioApi.signAndSend2(TradeioApi.java:223)
at Private.TradeioApi.PlaceOrder(TradeioApi.java:113)
at test.main.main(main.java:37)```
答案 0 :(得分:1)
我设法解决了这个问题。删除了此内容:
conn.getOutputStream().write(postDataBytes);
并发送如下请求:
try(OutputStream os = conn.getOutputStream()) {
os.write(postDataBytes);
}