old_flsystem/类库/Api.Framework/Cps/DtkApi.cs

231 lines
6.4 KiB
C#
Raw Normal View History

2022-09-20 03:10:29 +00:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Api.Framework.Cps
{
/// <summary>
/// 大淘客api
/// </summary>
public class DtkApi
{
private static DtkApi DTKApi = new DtkApi();
private DtkApi() { }
public static DtkApi GetDtkApi()
{
return DTKApi;
}
//老道的大淘客appkey
private string _appKey = "61a59957d79af";
private string _appSecret = "dce4f8ea16af4a920e4f07015cc62ecd";
public string SendDtk(string apiUrl, ApiParameters parameters, string version = "v1.0.0")
{
string result3;
try
{
string url = this.BuildUrl(apiUrl, version, parameters);
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = client.GetAsync(url).Result;
if (response.IsSuccessStatusCode)
{
Stream result2 = response.Content.ReadAsStreamAsync().Result;
StreamReader streamReader = new StreamReader(result2, Encoding.GetEncoding("utf-8"));
string result = streamReader.ReadToEnd();
streamReader.Close();
result2.Close();
result3 = result;
}
else
{
result3 = response.StatusCode.ToString();
}
}
}
catch (Exception ex)
{
result3 = ex.Message;
}
return result3;
}
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 = GetTimeStamp();
string result;
try
{
result = 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 = GetMd5Hash(md5, value);
}
return result;
}
private static string GetMd5Hash(MD5 md5Hash, string input)
{
byte[] array = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
StringBuilder sBuilder = new StringBuilder();
foreach (byte t in array)
{
sBuilder.Append(t.ToString("x2"));
}
return sBuilder.ToString();
}
}
public class ApiParameters
{
private List<KeyValuePair<string, string>> parameters = new List<KeyValuePair<string, string>>();
public List<KeyValuePair<string, string>> Value
{
get
{
return this.parameters;
}
}
public void Add(string key, string value)
{
this.parameters.Add(new KeyValuePair<string, string>(key, value));
}
public bool Delete(string key)
{
bool result;
try
{
int find = this.parameters.FindIndex((KeyValuePair<string, string> a) => a.Key == key);
if (find >= 0)
{
this.parameters.RemoveAt(find);
}
result = true;
}
catch
{
result = false;
}
return result;
}
public bool EditOrAdd(string key, string value)
{
bool result;
try
{
int find = this.parameters.FindIndex((KeyValuePair<string, string> a) => a.Key == key);
if (find >= 0)
{
this.parameters.RemoveAt(find);
}
this.Add(key, value);
result = true;
}
catch
{
result = false;
}
return result;
}
public bool Edit(string key, string value)
{
bool result;
try
{
int find = this.parameters.FindIndex((KeyValuePair<string, string> a) => a.Key == key);
if (find >= 0)
{
this.parameters.RemoveAt(find);
this.Add(key, value);
}
result = true;
}
catch
{
result = false;
}
return result;
}
public void Clear()
{
this.parameters.Clear();
}
}
}