gpt4 book ai didi

java - 无法将 "POST"请求设置为 HttpURLConnection 对象

转载 作者:行者123 更新时间:2023-11-30 01:44:22 24 4
gpt4 key购买 nike

我要用 java 连接 satang api 服务器。在以下代码中,con 对象无法设置“POST”请求。我不知道原因。请帮助我。

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/?"+req;
String signature=getSignature(req);
StringBuilder result = new StringBuilder();
URL url = new URL(baseUrl+operation);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput( true );
con.setInstanceFollowRedirects( false );
con.setRequestMethod("POST");

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){
System.out.println(con.getHeaderField("Allow"));
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();
}

最佳答案

您没有通过连接发送任何数据。 You have to use :

con.getOutputStream().write(...);

您应将 POST 请求有效负载发送到的位置 as bytes

关于java - 无法将 "POST"请求设置为 HttpURLConnection 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58714278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com