Java Can';“t集”;邮政「;对HttpURLConnection对象的请求

Java Can';“t集”;邮政「;对HttpURLConnection对象的请求,java,http,Java,Http,我将用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.

我将用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();
}

您没有通过连接发送任何数据:

您应该将POST请求有效负载发送到哪里我遇到了相同的问题。 我用下面的方法解决了这个问题。 第一。将“内容类型”设置为“应用程序/json”。
并将请求参数解析到您的帖子正文中。

这是我发现的,我不能肯定这是每个人的答案,但希望它能为其他许多人揭示问题

HttpsURLConnection实例的内部“method”参数似乎与实例上的requestMethod getter和setter关系不大。如果你像我一样,你会恢复使用android studio中的调试器,因为登录android是非常糟糕的。 在左边,我有一个调试检查器,显示URLConnection实例的内容。右边是“计算表达式”的结果

令人沮丧的是,对于这个问题(或许多其他安卓问题)没有任何明确的答案,当你试图找到答案时,往往会收到使用其他人库的邀请。这不是理想的解决方案,因为在许多情况下,使用其他人的库需要安全审查


希望这能有所帮助。在我的例子中,一切正常,但服务器返回了一个错误的请求响应,因为那里有一个bug。

有详细信息吗?代码执行的结果是什么?当您说您不能时,您的意思是什么?405错误意味着服务器限制端点可用的方法列表。它应该提供
Allow
标题,其中列出端点的可用方法。
GET
方法不太可能受到限制。服务器响应的
Allow
标题值是多少?为什么要将
内容类型设置为
application/x-www-form-urlencoded
,但不发送任何内容?您似乎试图使用查询字符串发送值,但没有
。您的URL是
BASEURLorders/amount=X&nonce=X&pair=X&price=X&side=X&type=limit
。您是否有该URL的处理程序?也许你的意思是
BASEURLorders/?amount=X&nonce=X&pair=X&price=X&side=X&type=limit
@JoshColeen尝试添加
System.out.println(con.getHeaderField(“允许”)
作为您的
的第一行,如果
body我有这种方式,但它是相同的
con.getOutputStream().write(...);
connection.getRequestMethod() //right side of image