poloniex后请求始终返回错误

时间:2018-12-25 03:07:07

标签: c# api post poloniex

我正在尝试编写一个简单的POST请求(returnBalances),但是它总是返回错误消息。

不知道我在做什么错。

解决了问题。该代码现已修复,可以正常工作。

代码如下:

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

1 个答案:

答案 0 :(得分:0)

该代码现已修复,可以正常工作。 我添加了HttpContent对象以显式定义请求的正文