赵庆明 发表于 2024-3-13 11:56:17

C# 调用API示例

本帖最后由 赵庆明 于 2024-5-24 09:57 编辑

签名实现public static string GetMd5Hash(string input)
    {
       var md5 = MD5.Create();
       var inputBytes = Encoding.ASCII.GetBytes(input);
       var hashBytes = md5.ComputeHash(inputBytes);
       var sb = new StringBuilder();
       foreach (var t in hashBytes)
      {
         sb.Append(t.ToString("x2"));
      }

      return sb.ToString();
    }
参数组装
    private FormUrlEncodedContent GetFormContent(List<KeyValuePair<string, string>>? data = null)
    {
      var requestTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString();
      var requestToken = GetMd5Hash(requestTime + GetMd5Hash(BtKey));

      if (data == null)
      {
            data = new List<KeyValuePair<string, string>>();
      }

      data.AddRange(new[]
      {
            new KeyValuePair<string, string>("request_time", requestTime),
            new KeyValuePair<string, string>("request_token", requestToken)
      });
      var formContent = new FormUrlEncodedContent(data);
      return formContent;
    }


部分C#代码实现
    private readonly string BtPanel;
    private readonly string BtKey;

static void Main(string[] args)
    {
      var url = BtPanel + "/safe/firewall/get_rules_list";
      
      var handler = new HttpClientHandler();
      handler.ServerCertificateCustomValidationCallback = delegate { return true; };
      var _httpClient = new HttpClient(handler);
      
      
      var response = _httpClient.PostAsync(url, GetFormContent()).Result;
      var result = response.Content.ReadAsStringAsync().Result;
      Console.WriteLine(result);
    }

赵庆明 发表于 2024-3-13 11:57:12

https://blog.csdn.net/u013667796/article/details/136675161
页: [1]
查看完整版本: C# 调用API示例