1

我已经构建了 java 代码来使用 satang “orders/user” api。但是出现405错误。请帮我。我已经建立了 placeLimitOrder() 函数。并且使用成功。然后 getUserOrders() 函数也使用相同的规则。但是这个函数得到了 405 错误。我找不到原因。

public String getUserOrders (String pair,String limit,String offset,String status,String side) {

    String req="limit="+limit+"&offset="+offset+"&pair="+pair+"&side="+side+"&status="+status;
    String operation="orders/user";
    String signature=getSignature(req);
    URL url = new URL(baseUrl+operation);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("POST");
    con.setDoOutput( true );
    con.setInstanceFollowRedirects( false );
    con.setRequestProperty("Authorization", "TDAX-API "+this.key);
    con.setRequestProperty("Signature",signature);
    con.setRequestProperty("Content-Type", "application/json"); 
    con.setRequestProperty("charset", "utf-8");
    con.setRequestProperty("User-Agent", "java client");
    con.setUseCaches( false );

    JsonObject obj=new JsonObject();
    obj.addProperty("limit", limit);
    obj.addProperty("offset", offset);
    obj.addProperty("pair", pair);
    obj.addProperty("side", side);
    obj.addProperty("status", status);

    String json=obj.toString();

    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(json);
    wr.flush();
    wr.close();

    int responseCode=con.getResponseCode();

    if(responseCode!=HttpURLConnection.HTTP_OK){
        throw new BadResponseException(responseCode);
    }
    BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));

    StringBuilder result = new StringBuilder();
    String line;
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
    rd.close();
    return result.toString();
}

以下代码运行良好。我找不到原因。

public String placeLimitOrder(String amount,String pair,String price,String side) throws IOException, BadResponseException
{
    Long lnonce=new Date().getTime();
    String nonce=lnonce.toString();
    String req="amount="+amount+"&nonce="+nonce+"&pair="+pair+"&price="+price+"&side="+side+"&type=limit";
    String operation="orders/";
    String signature=getSignature(req);
    URL url = new URL(baseUrl+operation);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET");
    con.setDoOutput( true );
    con.setInstanceFollowRedirects( false );

    con.setRequestProperty("Authorization", "TDAX-API "+this.key);
    con.setRequestProperty("Signature",signature);
    con.setRequestProperty("Content-Type", "application/json"); 
    con.setRequestProperty("charset", "utf-8");
    con.setRequestProperty("User-Agent", "java client");
    con.setUseCaches( false );

    JsonObject obj=new JsonObject();
    obj.addProperty("amount", amount);
    obj.addProperty("nonce", nonce);
    obj.addProperty("pair", pair);
    obj.addProperty("price", price);
    obj.addProperty("side", side);
    obj.addProperty("type", "limit");
    String json=obj.toString();

    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(json);
    wr.flush();
    wr.close();

    int responseCode=con.getResponseCode();

    if(responseCode!=HttpURLConnection.HTTP_OK){
        throw new BadResponseException(responseCode);
    }
    BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));

    StringBuilder result = new StringBuilder();
    String line;
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
    rd.close();
    return result.toString();
}
4

2 回答 2

1
public String getUserOrders(String pair,String limit,String offset,String status,String side) throws IOException, BadResponseException
{
    String req="limit="+limit+"&offset="+offset+"&pair="+pair+"&side="+side+"&status="+status;
    String operation="orders/user?"+req;
    String signature=getSignature("");
    URL url = new URL(baseUrl+operation);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("GET");
    con.setDoOutput( true );
    con.setInstanceFollowRedirects( false );

    con.setRequestProperty("Authorization", "TDAX-API "+this.key);
    con.setRequestProperty("Signature",signature);
    con.setRequestProperty("Content-Type", "application/json"); 
    con.setRequestProperty("charset", "utf-8");
    con.setRequestProperty("User-Agent", "java client");
    con.setUseCaches( false );

    int responseCode=con.getResponseCode();

    if(responseCode!=HttpURLConnection.HTTP_OK){
        throw new BadResponseException(responseCode);
    }
    BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));

    StringBuilder result = new StringBuilder();
    String line;
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
    rd.close();
    return result.toString();
}
于 2019-11-27T08:50:15.143 回答
0

因此,如果您尝试对https://api.tdax.com/api/orders/user进行GET操作, 标题应包括:

  • 签名
  • 验证

查询参数:

  • 一对
  • 限制
  • 抵消
  • 地位

从您的代码看来,您已将所有内容都放入签名中。

链接到 API 文档

这是您应该解决的问题:

  1. 网址。它应该看起来像:

https://api.tdax.com/api/orders/user?pair=pairValue&limit=limitValue&offset= ...添加所有参数并插入值。

  1. 将请求方法更改为GET
  2. 仔细检查您的标题(签名、身份验证)。

错误代码 405 -“错误请求”,这意味着您的请求 URL 很可能是错误的。修复 URL 后,您应该能够获得不同的响应代码,例如 403/401,这应该表明签名或身份验证有问题。在最好的情况下,您将获得 200,然后尝试解析 JSON 响应。

另外,我不建议你使用HttpURLConnection,一般不建议使用这个类,因为代码库很老旧,可能不支持新的HTTP/2标准,实际上配置起来非常困难使用这个类。请考虑使用OkHttpApache HttpClient或其他库。

于 2019-11-21T18:45:24.960 回答