Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java org.springframework.web.client.HttpClientErrorException$BadRequest:400错误请求二进制_Java_Resttemplate - Fatal编程技术网

Java org.springframework.web.client.HttpClientErrorException$BadRequest:400错误请求二进制

Java org.springframework.web.client.HttpClientErrorException$BadRequest:400错误请求二进制,java,resttemplate,Java,Resttemplate,我正试图使用RestTemplate将订单发布到binance。下一个简化代码: RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.add("user-agent", "Mozilla/5.0 " + "(Window

我正试图使用RestTemplate将订单发布到binance。下一个简化代码:

    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.add("user-agent",
            "Mozilla/5.0 " +
                    "(Windows NT 10.0; Win64; x64) " +
                    "AppleWebKit/537.36 (KHTML, like Gecko) " +
                    "Chrome/54.0.2840.99 " +
                    "Safari/537.36");
    headers.add(X_MBX_APIKEY, apiKey);

    HttpEntity<String> entity = new HttpEntity<>("parameters", headers);

    long time = new Date().getTime();
    String params1 = "symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1&price=0.005&recvWindow=50000" +
            "&timestamp=" + time;
    String signature = encodeSHA256(secretKey, params1);
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString("https://api.binance.com/api/v3/order/test")
            .queryParam("symbol", "LTCBTC")
            .queryParam("side", "BUY")
            .queryParam("type", "LIMIT")
            .queryParam("timeInForce", "GTC")
            .queryParam("quantity", "1")
            .queryParam("price", "0.005")
            .queryParam("recvWindow", "50000")
            .queryParam("timestamp", time)
            .queryParam("signature", signature);
    ResponseEntity<String> response = restTemplate.exchange(builder.build().toUri(), HttpMethod.POST, entity, String.class);

《邮递员》中的同样要求也很有效。那么这个错误的原因是什么呢?因为请求中的参数数量为9。也许binance api会将标头中的apiKey识别为第10个参数?提前谢谢。

我不知道restTemplate为什么不工作,但我从使用okhttp3的postman那里复制了代码。现在它开始工作了

        long time = new Date().getTime();
        String params = "symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1" +
        "&price=0.004&recvWindow=50000" +
                "&timestamp=" + time;
        String signature = encodeSHA256(marketKey, params);
        Request request = new Request.Builder()
                .url("https://api.binance.com/api/v3/order" +
                        "?symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1" +
                        "&price=0.004&recvWindow=50000" +
                        "&timestamp=" + time +
                        "&signature=" + signature)
                .post(new RequestBody() {
                    @Nullable
                    @Override
                    public MediaType contentType() {
                        return null;
                    }

                    @Override
                    public void writeTo(BufferedSink bufferedSink) throws IOException {

                    }
                })
                .addHeader("X-MBX-APIKEY", apiKey)
                .build();

我想向API所有者提出这个问题。
        long time = new Date().getTime();
        String params = "symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1" +
        "&price=0.004&recvWindow=50000" +
                "&timestamp=" + time;
        String signature = encodeSHA256(marketKey, params);
        Request request = new Request.Builder()
                .url("https://api.binance.com/api/v3/order" +
                        "?symbol=LTCBTC&side=BUY&type=LIMIT&timeInForce=GTC&quantity=1" +
                        "&price=0.004&recvWindow=50000" +
                        "&timestamp=" + time +
                        "&signature=" + signature)
                .post(new RequestBody() {
                    @Nullable
                    @Override
                    public MediaType contentType() {
                        return null;
                    }

                    @Override
                    public void writeTo(BufferedSink bufferedSink) throws IOException {

                    }
                })
                .addHeader("X-MBX-APIKEY", apiKey)
                .build();