post poloniex 请求总是 returns 错误

post poloniex request always returns error

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

不知道我做错了什么。

问题解决了。代码已修复,现在可以使用了。

代码如下:

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

代码已修复,现在可以使用了。 我添加了 HttpContent 对象来显式定义请求的主体