string bittrex_query(string path, NameValueCollection req, string key, string secret)
{
using (var client = new WebClient())
{
// nonce not really needed right now, can be also empty string
string nonce= ((int)(DateTime.Now.Subtract(new DateTime(1970,1,1,0,0,0, DateTimeKind.Utc))).TotalSeconds).ToString();
//add nonce and apiKey to req
req["apikey"] = key;
req["nonce"] = nonce;
// Convert from NameValueCollection to string ready for post.
var parameters = new StringBuilder();
foreach (string ReqKey in req.Keys)
parameters.AppendFormat("{0}={1}&", HttpUtility.UrlEncode(ReqKey), HttpUtility.UrlEncode(req[ReqKey]));
if (parameters.Length > 0)
parameters.Length -= 1;
string reqToString = parameters.ToString();
//make proper uri
string uri = "https://bittrex.com/api/v1.1/" + path + "?" + reqToString;
var headers = new NameValueCollection();
var keyByte = Encoding.UTF8.GetBytes(secret);
using (HMACSHA512 hmac = new HMACSHA512(keyByte))
{
var hash = hmac.ComputeHash(Encoding.UTF8.GetBytes(uri));
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
sBuilder.Append(hash[i].ToString("x2"));
headers["apisign"] = sBuilder.ToString();
}
client.Headers.Add(headers);
return client.DownloadString(uri);
}
}
And call it like that:
MessageBox.Show(bittrex_query(@"account/getbalances", new NameValueCollection(), textBoxAPIKEY.Text, textBoxSECRET.Text));
MessageBox.Show(bittrex_query(@"account/getbalance", new NameValueCollection { { "currency", "BTC" } }, textBoxAPIKEY.Text, textBoxSECRET.Text));
MessageBox.Show(bittrex_query(@"account/getdepositaddress", new NameValueCollection { { "currency", "BTC" } }, textBoxAPIKEY.Text, textBoxSECRET.Text));