System.Net.WebException: 'The remote server returned an error: (400) Bad Request.' When trying to create GET request

-1

I am trying to create a GET request to Binance api.

I get a Bad Request. I get an error in GetAccountInformation() method. According to Binance official documentation, URI I am creating looks exactly what I am getting.

    public static string GetAccountInformation()
    {
        string result = string.Empty;
        string accountInformationUri = GetAccountInformationUri();
        string url = string.Format("https://api.binance.com/api/v3/account?{0}", accountInformationUri);

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        request.Headers.Add("X-MBX-APIKEY", "keygoeshere");
        request.Method = "GET";

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        using (Stream stream = response.GetResponseStream())
        using (StreamReader reader = new StreamReader(stream))
        {
            result = reader.ReadToEnd();
        }

        return result;
    }


    private static string GetAccountInformationUri()
    {
        int timestamp = GetTimestamp();

        string totalParams = String.Format("timestamp={0}", timestamp);
        string key = "keygoeshere";

        string signature = Encode(totalParams, key);
        string result = String.Format("{0}&signature={1}", totalParams, signature);

        return result;
    }

    private static int GetTimestamp()
    {
        TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);
        int result = (int)t.TotalSeconds;
        return result;
    }

    public static byte[] Encode(byte[] key, byte[] message)
    {
        var hash = new HMACSHA256(key);
        var result = hash.ComputeHash(message);
        return result;
    }

    public static string Encode(string input, string key)
    {
        byte[] inputByteArray = System.Text.Encoding.UTF8.GetBytes(input);
        byte[] keyByteArray = System.Text.Encoding.ASCII.GetBytes(key);

        byte[] encodedResult = Encode(keyByteArray, inputByteArray);

        var sBuilder = new StringBuilder();

        for (int i = 0; i < encodedResult.Length; i++)
        {
            sBuilder.Append(encodedResult[i].ToString("x2"));
        }

        string result = sBuilder.ToString();

        return result;
    }

Probably I did something wrong in creating GET method. Like not providing a proper URL for binance api.

As requested, I am adding exception details :

>

 System.Net.WebException
  HResult=0x80131509
  Message=The remote server returned an error: (400) Bad Request.
  Source=System
  StackTrace:
   at System.Net.HttpWebRequest.GetResponse()
   at Binance.Api.GetAccountInformation() in C:\Users\gugat\Source\Repos\cnm\Classes\Exchange\Binance.cs:line 41
   at Binance.Tests.ApiTests.GetAccountInformationTest() in C:\Users\gugat\Source\Repos\cnm\Test projects\UnitTest\Classes\Exchange\ApiTests.cs:line 17

enter image description here

c#
json
binance
asked on Stack Overflow Jan 14, 2020 by Guga Todua • edited Jan 14, 2020 by Amy

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0