Advertisement
DmT

BTC-E API class

DmT
Aug 4th, 2012
2,756
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. /*
  2.  * Base for making api class for btc-e.com
  3.  * DmT
  4.  * 2012
  5.  */
  6.  
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.IO;
  12. using System.Net;
  13. using System.Security.Cryptography;
  14.  
  15. namespace BtceTestApp
  16. {
  17.     class BtceApi
  18.     {
  19.         string key;
  20.         HMACSHA512 hashMaker;
  21.  
  22.         public BtceApi(string key, string secret)
  23.         {
  24.             this.key = key;
  25.             hashMaker = new HMACSHA512(Encoding.ASCII.GetBytes(secret));
  26.         }
  27.  
  28.         public string GetInfo()
  29.         {
  30.             return Query(new Dictionary<string, string>()
  31.             {
  32.                 { "method", "getInfo" }
  33.             });
  34.         }
  35.  
  36.         string Query(Dictionary<string, string> args)
  37.         {
  38.             args.Add("nonce", GetNonce().ToString());
  39.  
  40.             var dataStr = BuildPostData(args);
  41.             var data = Encoding.ASCII.GetBytes(dataStr);
  42.  
  43.             var request = WebRequest.Create(new Uri("https://btc-e.com/tapi")) as HttpWebRequest;
  44.             if (request == null)
  45.                 throw new Exception("Non HTTP WebRequest");
  46.  
  47.             request.Method = "POST";
  48.             request.Timeout = 15000;
  49.             request.ContentType = "application/x-www-form-urlencoded";
  50.             request.ContentLength = data.Length;
  51.  
  52.             request.Headers.Add("Key", key);
  53.             var sign = ByteArrayToString(hashMaker.ComputeHash(data)).ToLower();
  54.             request.Headers.Add("Sign", sign);
  55.             var reqStream = request.GetRequestStream();
  56.             reqStream.Write(data, 0, data.Length);
  57.             reqStream.Close();
  58.  
  59.             var response = request.GetResponse();
  60.             var resStream = response.GetResponseStream();
  61.             var resStreamReader = new StreamReader(resStream);
  62.             var resString = resStreamReader.ReadToEnd();
  63.  
  64.             return resString;
  65.         }
  66.  
  67.         static string ByteArrayToString(byte[] ba)
  68.         {
  69.             string hex = BitConverter.ToString(ba);
  70.             return hex.Replace("-", "");
  71.         }
  72.  
  73.         static string BuildPostData(Dictionary<string, string> d)
  74.         {
  75.             string s = "";
  76.             for (int i = 0; i < d.Count; i++)
  77.             {
  78.                 var item = d.ElementAt(i);
  79.                 var key = item.Key;
  80.                 var val = item.Value;
  81.  
  82.                 s += String.Format("{0}={1}", key, val);
  83.  
  84.                 if (i != d.Count - 1)
  85.                     s += "&";
  86.             }
  87.             return s;
  88.         }
  89.  
  90.         static DateTime unixEpoch = new DateTime(1970, 1, 1);
  91.         int GetNonce()
  92.         {
  93.             var now = DateTime.UtcNow;
  94.             var dif = now - unixEpoch;
  95.             return (int)dif.TotalSeconds;
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement