0

我正在尝试编写一个简单的 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;
        }
    }
4

1 回答 1

0

代码已修复,现在可以正常工作。我添加了 HttpContent 对象来明确定义请求的主体

于 2018-12-27T16:14:14.743 回答