无法从 HttpURLConnection POST 方法获得响应

Unable to get response from HttpURLConnection POST method

我已经看到关于这个问题的多个线程,但不幸的是我无法从他们那里弄清楚如何解决我的问题。 需要帮助找出如何修复我的 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)```

我设法解决了这个问题。删除了这个:

conn.getOutputStream().write(postDataBytes);

并像这样发送请求:

try(OutputStream os = conn.getOutputStream()) {
            os.write(postDataBytes);
        }