当前位置:论坛首页 > 第三方应用 > Linux面板插件

C# 调用API示例

发表在 第三方应用2024-3-13 11:56 [复制链接] 1 266

签名实现
  1. public static string GetMd5Hash(string input)
  2.     {
  3.        var md5 = MD5.Create();
  4.        var inputBytes = Encoding.ASCII.GetBytes(input);
  5.        var hashBytes = md5.ComputeHash(inputBytes);
  6.        var sb = new StringBuilder();
  7.        foreach (var t in hashBytes)
  8.       {
  9.            sb.Append(t.ToString("x2"));
  10.       }

  11.       return sb.ToString();
  12.     }
复制代码
参数组装
  1.     private FormUrlEncodedContent GetFormContent(List<KeyValuePair<string, string>>? data = null)
  2.     {
  3.         var requestTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();
  4.         var requestToken = GetMd5Hash(requestTime + GetMd5Hash(BtKey));

  5.         if (data == null)
  6.         {
  7.             data = new List<KeyValuePair<string, string>>();
  8.         }

  9.         data.AddRange(new[]
  10.         {
  11.             new KeyValuePair<string, string>("request_time", requestTime),
  12.             new KeyValuePair<string, string>("request_token", requestToken)
  13.         });
  14.         var formContent = new FormUrlEncodedContent(data);
  15.         return formContent;
  16.     }
复制代码


部分C#代码实现
  1.     private readonly string BtPanel;
  2.     private readonly string BtKey;
  3.     private static HttpClient _httpClient = new HttpClient();
  4.    
  5. [HttpGet]
  6.     public string MyFireWall()
  7.     {
  8.         var url = BtPanel + "/safe/firewall/get_rules_list";
  9.         var response = _httpClient.PostAsync(url, GetFormContent()).Result;
  10.         var result = response.Content.ReadAsStringAsync().Result;
  11.         return result;
  12.     }
复制代码



原文链接 原文链接

使用道具 举报 只看该作者 回复
发表于 2024-3-13 11:57:12 | 显示全部楼层
https://blog.csdn.net/u013667796/article/details/136675161
使用道具 举报 回复 支持 反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

企业版年付运维跟进群

普通问题处理

论坛响应时间:72小时

问题处理方式:排队(仅解答)

工作时间:白班:9:00 - 18:00

紧急问题处理

论坛响应时间:10分钟

问题处理方式:1对1处理(优先)

工作时间:白班:9:00 - 18:00

工作时间:晚班:18:00 - 24:00

立即付费处理

工作时间:09:00至24:00

快速回复 返回顶部 返回列表