using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using System.IO; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; using System.Net.Security; using Newtonsoft.Json; using System.Web; //Add reference to System.Web using Newtonsoft.Json.Linq; //"Manage NuGet packages" -> Search for "newtonsoft json". -> click "install". namespace OkChanger { /* { var method = new OkChanger.API.MethodCall(OkChanger.API.API_Methods.Rating_PaySystems, 21241, "API_KEY_HERE"); method.Execute(); //Console.WriteLine(method.Result.ToString()); } { var method = new OkChanger.API.MethodCall(OkChanger.API.API_Methods.Rating_Exchangers, 21241, "API_KEY_HERE"); //method.AddParameter("paySystemID", 10);//OKPAY method.AddParameter("paySystemID", 0);//ALL Exhangers method.Execute(); //Console.WriteLine(method.Result.ToString()); } { var method = new OkChanger.API.MethodCall(OkChanger.API.API_Methods.Rating_Exchangers_Directions, 21241, "API_KEY_HERE"); method.AddParameter("fromPaySystemID", 10);//OKPAY method.AddParameter("fromCurrencyName", "USD"); method.AddParameter("toPaySystemID", 10);//OKPAY method.AddParameter("toCurrencyName", "EUR"); method.Execute(); //Console.WriteLine(method.Result.ToString()); } { var method = new OkChanger.API.MethodCall(OkChanger.API.API_Methods.Rating_Directions_From, 21241, "API_KEY_HERE"); method.AddParameter("fromPaySystemID", 10);//OKPAY method.AddParameter("fromCurrencyName", "USD"); method.Execute(); //Console.WriteLine(method.Result.ToString()); } { var method = new OkChanger.API.MethodCall(OkChanger.API.API_Methods.Rating_Directions_To, 21241, "API_KEY_HERE"); method.AddParameter("toPaySystemID", 10);//OKPAY method.AddParameter("toCurrencyName", "USD"); method.Execute(); //Console.WriteLine(method.Result.ToString()); } */ public class API { public enum API_Methods { Rating_PaySystems, Rating_Exchangers_Directions, Rating_Exchangers, Rating_Directions_From, Rating_Directions_To, } public class MethodCall { const string APIURL = @"https://www.OkChanger.com/API/"; public long? APIID { get; set; } public string APIKey { get; set; } public string MethodName { get; set; } public JToken Result { get; private set; } private Dictionary Parameters = new Dictionary(); public MethodCall(API_Methods method, long? APIID = null, string APIKey = null) : this(method.ToString(), APIID, APIKey) { } public MethodCall(string methodName, long? APIID = null, string APIKey = null) { this.MethodName = methodName; this.APIID = APIID; this.APIKey = APIKey; if (this.APIID.HasValue) { this.AddParameter("APIID", APIID); } } public void AddParameter(string name, object value) { Parameters.Add(name, value.ToString()); } public void AddParameter(string name, long value) { Parameters.Add(name, value.ToString()); } public void AddParameter(string name, int value) { Parameters.Add(name, value.ToString()); } public void AddParameter(string name, short value) { Parameters.Add(name, value.ToString()); } public void AddParameter(string name, byte value) { Parameters.Add(name, value.ToString()); } public void AddParameter(string name, float value) { Parameters.Add(name, value.ToString("0.00000000", System.Globalization.CultureInfo.InvariantCulture.NumberFormat)); } public void AddParameter(string name, decimal value) { Parameters.Add(name, value.ToString("0.00000000", System.Globalization.CultureInfo.InvariantCulture.NumberFormat)); } public void Execute() { var tmpParameters = this.Parameters.ToDictionary(c => c.Key, c => c.Value); var values = tmpParameters.OrderBy(c => c.Key).Select(c => c.Value).ToList(); if (!string.IsNullOrEmpty(this.APIKey)) { values.Add(this.APIKey); var original = string.Join(":", values); tmpParameters.Add("signature", API.GetMd5Hash(original)); } string url = APIURL + MethodName + "?" + string.Join("&", tmpParameters.Select(c => HttpUtility.UrlEncode(c.Key) + "=" + HttpUtility.UrlEncode(c.Value))); string result = null; try { using (WebClient wc = new WebClient()) { wc.Encoding = System.Text.UTF8Encoding.UTF8; #if DEBUG System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); #endif result = wc.DownloadString(url); #if DEBUG sw.Stop(); Console.WriteLine("API Call Method: {0}, {1} ms", MethodName, sw.ElapsedMilliseconds); #endif } } catch { throw new OkChangerAPIConnectionException(); } JObject jObj; string errorMessage; try { jObj = JObject.Parse(result); errorMessage = (string)jObj["ErrorMessage"]; } catch { throw new OkChangerAPIInvalidAnswerException(); } if (!string.IsNullOrEmpty(errorMessage)) { throw new OkChangerAPIException(errorMessage); } this.Result = jObj["Result"]; } } internal static string GetMd5Hash(string input) { using (var md5 = System.Security.Cryptography.MD5.Create()) { var arr = System.Text.Encoding.ASCII.GetBytes(input); arr = md5.ComputeHash(arr); return BitConverter.ToString(arr).Replace("-", ""); } } } public class OkChangerAPIConnectionException : Exception { } public class OkChangerAPIInvalidAnswerException : Exception { } public class OkChangerAPIException : Exception { public OkChangerAPIException(string message) : base(message) { } } }