Java 获取400个错误 ;发送邮件时的代码 ;请访问www.okex.com

Java 获取400个错误 ;发送邮件时的代码 ;请访问www.okex.com,java,http,post,Java,Http,Post,我需要一些帮助,因为我不知道如何使用vanilla java发送我的POST HTTP请求。我得到一个错误代码400。因此,我首先使用一个生成正确请求的客户端,并使用wireshark嗅探请求 工作客户端创建的请求来自: 由我的Java客户端创建的请求 Hypertext Transfer Protocol POST /api/futures/v3/order_algo HTTP/1.1\r\n Accept: application/json\r\n Cookie: locale=en_US\

我需要一些帮助,因为我不知道如何使用vanilla java发送我的POST HTTP请求。我得到一个错误代码400。因此,我首先使用一个生成正确请求的客户端,并使用wireshark嗅探请求

工作客户端创建的请求来自:

由我的Java客户端创建的请求

Hypertext Transfer Protocol
POST /api/futures/v3/order_algo HTTP/1.1\r\n
Accept: application/json\r\n
Cookie: locale=en_US\r\n
OK-ACCESS-KEY: XXX\r\n
OK-ACCESS-SIGN: WpAB/qDr0CGNta4DvVBJ9HWWb1mIr1kFq/vOYYhOXGI=\r\n
OK-ACCESS-TIMESTAMP: 2020-09-30T12:48:57.730Z\r\n
OK-ACCESS-PASSPHRASE: XXX\r\n
Content-Type: application/json; charset=UTF-8\r\n
User-Agent: okhttp/4.7.2\r\n
Host: www.okex.com\r\n
Connection: keep-alive\r\n
Content-Length: 121\r\n
\r\n
[Full request URI: http://www.okex.com/api/futures/v3/order_algo]
[HTTP request 1/1]
[Response in frame: 20]
File Data: 121 bytes`
所以这两个请求看起来很相似,但我仍然从服务器得到一个错误代码400。 请看我的java代码。我的输入函数是getJSONObject:

private Object getJSONObject(String requestPath,String body,String requestType)
{
    HttpsURLConnection con = send(StaticParameter.URL_PREX,StaticParameter.API_VERSION,requestType,requestPath,body);
    String returnString = receive(con);
    return JSONObject.parse(returnString);
}

private HttpsURLConnection send(String urlPrefix,String apiVersion,String requestType,String requestPath,String body)
{
    String httpsUrl = urlPrefix+apiVersion+requestPath;
    URL  url;
    HttpsURLConnection con = null;
    
    try
    {
        String timeStampold = LocalDateTime.now(ZoneOffset.UTC).format(StaticParameter.ISO_801);
        DateTimeFormatter formater = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",Locale.ENGLISH);
        String timeStamp = LocalDateTime.now(ZoneOffset.UTC).format(formater);
        timeStamp = "2020-09-30T12:48:57.730Z";
          
        String preHash = timeStamp + requestType + apiVersion + requestPath + body;
        String sign = Base64.getEncoder().encodeToString(mac.doFinal(preHash.getBytes(StandardCharsets.UTF_8)));
        byte[] postDataBytes = body.getBytes(StandardCharsets.UTF_8);

        url = new URL(httpsUrl);             
        con = (HttpsURLConnection)url.openConnection();
        con.setRequestMethod(requestType);
        con.setFixedLengthStreamingMode(postDataBytes.length);
        con.setDoOutput(true);
        con.setRequestProperty("Accept","application/json");
        con.setRequestProperty("Cookie", "locale=en_US");            
        con.setRequestProperty("OK-ACCESS-KEY",security.getApiKey());
        con.setRequestProperty("OK-ACCESS-SIGN", sign);          
        con.setRequestProperty("OK-ACCESS-TIMESTAMP", timeStamp);
        con.setRequestProperty("OK-ACCESS-PASSPHRASE", security.getPassPhrase());
        //con.setRequestProperty("content-type","application/x-www-form-urlencoded");
        con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        con.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
        con.setRequestProperty("Connection","Keep-Alive");
        //con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
        con.setRequestProperty("User-Agent", "okhttp/4.7.2");
        con.connect();


        if(requestType.equals(StaticParameter.POST))
        {
            try(OutputStream os = con.getOutputStream()) 
            {
                //os.writeBytes(body);
                os.write(postDataBytes,0, postDataBytes.length);
            }
        }
    }
    catch(Exception e)
    {
        Messenger.update(StaticParameter.ErrorLogEvent,e.toString());
    }

    return con;
}

private String receive(HttpsURLConnection con)
{
    String returnString = "";

    if(con != null)
    {
        BufferedReader br = null;
        InputStream inputStream = null;
        try 
        {
            //System.out.println("****** Content of the URL ********");
            inputStream = con.getInputStream();
            br = new BufferedReader(new InputStreamReader(inputStream));

            String input;

            while ((input = br.readLine()) != null)
            {
                returnString += input;
                System.out.println(input);
            }                   
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
            Messenger.update(StaticParameter.ErrorLogEvent,e.toString());
        }
        finally
        {
            try
            {                   
                inputStream.close();
                br.close();
            }   
            catch(Exception e)
            {
            }
        }
    }

    return returnString;
}
调用con.getInputStream()函数时,得到错误代码400。我不能使用基于okhttp和改型的工作客户机的原因是,在运行了几天之后,在进行了几次例外之后,我遇到了一个堆内存错误,所以我宁愿用vanilla java简化它。 提前感谢您的帮助。 `

private Object getJSONObject(String requestPath,String body,String requestType)
{
    HttpsURLConnection con = send(StaticParameter.URL_PREX,StaticParameter.API_VERSION,requestType,requestPath,body);
    String returnString = receive(con);
    return JSONObject.parse(returnString);
}

private HttpsURLConnection send(String urlPrefix,String apiVersion,String requestType,String requestPath,String body)
{
    String httpsUrl = urlPrefix+apiVersion+requestPath;
    URL  url;
    HttpsURLConnection con = null;
    
    try
    {
        String timeStampold = LocalDateTime.now(ZoneOffset.UTC).format(StaticParameter.ISO_801);
        DateTimeFormatter formater = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",Locale.ENGLISH);
        String timeStamp = LocalDateTime.now(ZoneOffset.UTC).format(formater);
        timeStamp = "2020-09-30T12:48:57.730Z";
          
        String preHash = timeStamp + requestType + apiVersion + requestPath + body;
        String sign = Base64.getEncoder().encodeToString(mac.doFinal(preHash.getBytes(StandardCharsets.UTF_8)));
        byte[] postDataBytes = body.getBytes(StandardCharsets.UTF_8);

        url = new URL(httpsUrl);             
        con = (HttpsURLConnection)url.openConnection();
        con.setRequestMethod(requestType);
        con.setFixedLengthStreamingMode(postDataBytes.length);
        con.setDoOutput(true);
        con.setRequestProperty("Accept","application/json");
        con.setRequestProperty("Cookie", "locale=en_US");            
        con.setRequestProperty("OK-ACCESS-KEY",security.getApiKey());
        con.setRequestProperty("OK-ACCESS-SIGN", sign);          
        con.setRequestProperty("OK-ACCESS-TIMESTAMP", timeStamp);
        con.setRequestProperty("OK-ACCESS-PASSPHRASE", security.getPassPhrase());
        //con.setRequestProperty("content-type","application/x-www-form-urlencoded");
        con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        con.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
        con.setRequestProperty("Connection","Keep-Alive");
        //con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
        con.setRequestProperty("User-Agent", "okhttp/4.7.2");
        con.connect();


        if(requestType.equals(StaticParameter.POST))
        {
            try(OutputStream os = con.getOutputStream()) 
            {
                //os.writeBytes(body);
                os.write(postDataBytes,0, postDataBytes.length);
            }
        }
    }
    catch(Exception e)
    {
        Messenger.update(StaticParameter.ErrorLogEvent,e.toString());
    }

    return con;
}

private String receive(HttpsURLConnection con)
{
    String returnString = "";

    if(con != null)
    {
        BufferedReader br = null;
        InputStream inputStream = null;
        try 
        {
            //System.out.println("****** Content of the URL ********");
            inputStream = con.getInputStream();
            br = new BufferedReader(new InputStreamReader(inputStream));

            String input;

            while ((input = br.readLine()) != null)
            {
                returnString += input;
                System.out.println(input);
            }                   
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
            Messenger.update(StaticParameter.ErrorLogEvent,e.toString());
        }
        finally
        {
            try
            {                   
                inputStream.close();
                br.close();
            }   
            catch(Exception e)
            {
            }
        }
    }

    return returnString;
}