heyuntao
2023-05-04 fbd6a11a99051f425640bf352842f4a0ecaa7a4d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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<T>(IDkRequest<T> 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;
        }
    }
 
}