Reputation: 77
I'm going to connect satang api server with java. In the following code con object can't set "POST" request. I don't know the reason. Please help me.
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();
}
Upvotes: 2
Views: 1891
Reputation: 315
This is what I have found, I can't say with certainty it is the answer for everyone but hopefully it will shed some light on the problem for many others.
It seems that the internal "method" parameter of the HttpsURLConnection instance IS NOT very related to the requestMethod getter and setter on the instance. If you are like me you revert to using the debugger in android studio because logging in android is abysmal. On the left I have the Debug inspector showing the contents of the URLConnection instance. On the right I have the results of "Evaluate Expression" on
connection.getRequestMethod() //right side of image
It is frustrating that there are not any definitive answers on this (or many other android questions) and when you try to find them you are often greeted with invitations to use someone else's library. This is not the ideal solution because in many cases using someone else's library requires security vetting.
Hopefully this helps. In my case, things were working correctly but the server was sending back a BAD_REQUEST response because of a bug over there.
Upvotes: 2
Reputation:
I've met same problem. I've solved this problem with following method. First. Make your "Content-Type" to "application/json". And parse the request params to your post body.
Upvotes: -1
Reputation: 8676
You are not sending any data through your connection. You have to use:
con.getOutputStream().write(...);
where you shold send your POST request payload as bytes
Upvotes: 2