简体   繁体   中英

post poloniex request always returns error

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;
        }
    }

the code is fixed to work now. I added the HttpContent object to explicitly define the body of the request

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM