using System; using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Security.Cryptography; using System.Text; namespace DkSdkCore { public class DtkApp { private string _appKey = ""; private string _appSecret = ""; public DtkApp() { //if (string.IsNullOrEmpty(appKey) || string.IsNullOrEmpty(appSecret)) //{ // Console.WriteLine("AppKey 和 AppSecret 必填!"); // throw new Exception("AppKey 和 AppSecret 必填!"); //} //this._appKey = appKey; //this._appSecret = appSecret; this._appKey = "641129ab40767"; this._appSecret = "337944d910dcff5ba0ad331a4694b4b5"; } public string Excute(IDkRequest request, string version = "v1.2.4") where T:DkResponse { return Get(request.GetUrl(), request.GetParameters(),version); } public string Get(string apiUrl, ApiParameters parameters,string version = "v1.2.4") { string result2; try { string url = this.BuildUrl(apiUrl, version, parameters); using (HttpClient client = new HttpClient()) { HttpResponseMessage response = client.GetAsync(url).Result; if (response.IsSuccessStatusCode) { Stream expr_35 = response.Content.ReadAsStreamAsync().Result; StreamReader expr_45 = new StreamReader(expr_35, Encoding.GetEncoding("utf-8")); string result = expr_45.ReadToEnd(); expr_45.Close(); expr_35.Close(); result2 = result; } else { result2 = response.StatusCode.ToString(); } } } catch (Exception arg_7E_0) { result2 = arg_7E_0.Message; } return result2; } private string BuildUrl(string apiUrl, string version, ApiParameters parameters) { int nonce; string timer; string sign = this.MakeSign(out nonce, out timer); if (sign == "") { return ""; } string url = string.Concat(new string[] { apiUrl.Trim(), "?appKey=", this._appKey.Trim(), "&nonce=", nonce.ToString(), "&signRan=", sign, "&version=", version.Trim(), "&timer=", timer }); if (parameters.Value.Count <= 0) { return url; } for (int i = 0; i <= parameters.Value.Count - 1; i++) { url += string.Format("&{0}={1}", parameters.Value[i].Key, parameters.Value[i].Value); } return url; } private string MakeSign(out int nonce, out string timer) { Random rd = new Random(); nonce = rd.Next(100000, 1000000); timer = DtkApp.GetTimeStamp(); string result; try { result = DtkApp.Md5(string.Format("appKey={0}&timer={1}&nonce={2}&key={3}", new object[] { this._appKey, timer, nonce, this._appSecret })).ToUpper(); } catch (Exception) { result = ""; } return result; } private static string GetTimeStamp() { return Convert.ToInt64((DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalMilliseconds).ToString(); } private static string Md5(string value) { string result = string.Empty; if (string.IsNullOrEmpty(value)) { return result; } using (MD5 md5 = MD5.Create()) { result = DtkApp.GetMd5Hash(md5, value); } return result; } private static string GetMd5Hash(MD5 md5Hash, string input) { byte[] arg_17_0 = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input)); StringBuilder sBuilder = new StringBuilder(); byte[] array = arg_17_0; for (int i = 0; i < array.Length; i++) { byte t = array[i]; sBuilder.Append(t.ToString("x2")); } return sBuilder.ToString(); } private static bool VerifyMd5Hash(MD5 md5Hash, string input, string hash) { string hashOfInput = DtkApp.GetMd5Hash(md5Hash, input); StringComparer comparer = StringComparer.OrdinalIgnoreCase; return comparer.Compare(hashOfInput, hash) == 0; } } }