Reputation: 1
I'm trying to write a simple POST request (returnBalances), but it always returns error message.
Can't figure out what I'm doing wrong.
Solved the problem. The code is fixed to work now.
Here is the code:
public String checkBalancesRequest()
{
uint Nonce = requestCounter++;
String Url = "https://poloniex.com/tradingApi";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, Url);
Dictionary<string,string> body= new Dictionary<string,string>();
body.Add("command", "returnBalances");
body.Add("nonce",Nonce.ToString());
HttpContent cont = new FormUrlEncodedContent(body);
String stringToSign = cont.ReadAsStringAsync().Result;
String signature = SHA512Sign(stringToSign, secretView);
request.Content = cont;
request.Headers.Add("Key", keyView);
request.Headers.Add("Sign", signature);
dynamic result2 = client.SendAsync(request).Result;
String instrs = "";
if (result2.IsSuccessStatusCode) { instrs = result2.Content.ReadAsStringAsync().Result.ToString(); if (instrs == "{\"error\":\"Invalid command.\"}") { instrs = null; } } else { instrs = null; }
return instrs;
}
public static String SHA512Sign(String message, String key)
{
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(message)))
{
var signed = new HMACSHA512(Encoding.UTF8.GetBytes(key)).ComputeHash(stream)
.Aggregate(new StringBuilder(), (sb, b) => sb.AppendFormat("{0:x2}", b), (sb) => sb.ToString());
return signed;
}
}
Upvotes: 0
Views: 104
Reputation: 1
the code is fixed to work now. I added the HttpContent object to explicitly define the body of the request
Upvotes: 0