对satang api的私人http请求

问题描述 投票:0回答:1

我要请求使用satang私人api我已经编写了Java代码。公开请求有效,但遵循私人请求无效。但是发生401错误。

public String getUserInfo() throws IOException, BadResponseException {

    String operation="users/:"+this.id;
    String signature=getSignature(":"+this.id);
    StringBuilder result = new StringBuilder();
    URL url = new URL(baseUrl+operation);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setDoOutput( true );
    con.setInstanceFollowRedirects( false );
    con.setRequestMethod( "GET" );
    con.setRequestProperty("Authorization", "TDAX-API "+this.key);
    con.setRequestProperty("Signature",signature);
    con.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded"); 
    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()));

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

请帮助我这个问题。

java api http request private
1个回答
1
投票
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("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("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();
}
© www.soinside.com 2019 - 2024. All rights reserved.