HttpHelper.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using Microsoft.Extensions.Primitives;
  10. using Newtonsoft.Json;
  11. using Newtonsoft.Json.Linq;
  12. namespace ET.Server
  13. {
  14. public static class HttpHelper
  15. {
  16. public static async ETTask<string> GetBodyParameter(HttpListenerContext context)
  17. {
  18. return await new StreamReader(context.Request.InputStream).ReadToEndAsync();
  19. }
  20. public static void Response(HttpListenerContext context, object response)
  21. {
  22. byte[] bytes = JsonHelper.ToJson(response).ToUtf8();
  23. context.Response.StatusCode = 200;
  24. context.Response.ContentEncoding = Encoding.UTF8;
  25. context.Response.ContentLength64 = bytes.Length;
  26. context.Response.OutputStream.Write(bytes, 0, bytes.Length);
  27. }
  28. /// <summary>
  29. /// post请求抖音接口
  30. /// </summary>
  31. /// <param name="url"></param>
  32. /// <param name="heads"></param>
  33. /// <param name="param"></param>
  34. /// <returns></returns>
  35. public static string PostRequestByDouyin(string url, Dictionary<string, string> heads, JObject param)
  36. {
  37. if (string.IsNullOrEmpty(url) || param is not { Count: > 0 })
  38. {
  39. return null;
  40. }
  41. using HttpClient httpClient = new HttpClient();
  42. httpClient.DefaultRequestHeaders.Accept.TryParseAdd("application/json");
  43. // 设置请求头
  44. if (heads is { Count: >= 0 })
  45. {
  46. foreach (var head in heads)
  47. {
  48. httpClient.DefaultRequestHeaders.Add(head.Key, head.Value);
  49. }
  50. }
  51. // post
  52. HttpContent content = new StringContent(JsonConvert.SerializeObject(param), Encoding.UTF8, "application/json");
  53. // response
  54. HttpResponseMessage response = httpClient.PostAsync(new Uri(url), content).Result;
  55. if (response.IsSuccessStatusCode)
  56. {
  57. return response.Content.ReadAsStringAsync().Result;
  58. }
  59. string statusCode = response.StatusCode.ToString();
  60. Log.Debug($"http请求错误...statusCode:{statusCode}, url:{url}");
  61. return null;
  62. }
  63. /// <summary>
  64. /// get请求抖音接口
  65. /// </summary>
  66. /// <param name="baseUrl"></param>
  67. /// <param name="heads"></param>
  68. /// <param name="param"></param>
  69. /// <returns></returns>
  70. public static string GetRequestByDouyin(string baseUrl, Dictionary<string, string> heads, Dictionary<string, string> param)
  71. {
  72. if (string.IsNullOrEmpty(baseUrl) || param is not { Count: > 0 })
  73. {
  74. return null;
  75. }
  76. using HttpClient httpClient = new HttpClient();
  77. httpClient.DefaultRequestHeaders.Accept.TryParseAdd("application/json");
  78. // 设置请求头
  79. if (heads is { Count: >= 0 })
  80. {
  81. foreach (var head in heads)
  82. {
  83. httpClient.DefaultRequestHeaders.Add(head.Key, head.Value);
  84. }
  85. }
  86. StringBuilder url = new StringBuilder(baseUrl);
  87. if (param is { Count: >= 0 })
  88. {
  89. url.Append('?');
  90. List<string> keyList = new List<string>(param.Keys);
  91. for (int i = 0; i < param.Count; i++)
  92. {
  93. url.Append(keyList[i]).Append('=').Append(param[keyList[i]]);
  94. if (i < param.Count)
  95. {
  96. url.Append('&');
  97. }
  98. }
  99. }
  100. HttpResponseMessage response = httpClient.GetAsync(new Uri(url.ToString())).Result;
  101. if (response.IsSuccessStatusCode)
  102. {
  103. return response.Content.ReadAsStringAsync().Result;
  104. }
  105. string statusCode = response.StatusCode.ToString();
  106. Log.Debug($"http请求错误...statusCode:{statusCode}, url:{url}");
  107. return null;
  108. }
  109. /// <summary>
  110. /// 签名验证
  111. /// </summary>
  112. /// <param name="header"> = {
  113. /// "x-nonce-str": "123456",
  114. /// "x-timestamp": "456789",
  115. /// "x-roomid": "268",
  116. /// "x-msg-type": "live_gift",
  117. /// } </param>
  118. /// <param name="bodyStr"> = "abc123你好"</param>
  119. /// <param name="secret"> = "oumuamua410"</param>
  120. /// <returns>PDcKhdlsrKEJif6uMKD2dw==</returns>
  121. public static string Signature(Dictionary<string, string> header, string bodyStr, string secret)
  122. {
  123. List<string> keyList = new List<string>(4);
  124. keyList.AddRange(header.Select(keyValuePair => keyValuePair.Key));
  125. keyList.Sort();
  126. List<string> kvList = new List<string>(4);
  127. kvList.AddRange(keyList.Select(key => key + "=" + header[key]));
  128. string urlParams = string.Join("&", kvList);
  129. string rawData = urlParams + bodyStr + secret;
  130. byte[] hashBytes = MD5.HashData(Encoding.UTF8.GetBytes(rawData));
  131. return Convert.ToBase64String(hashBytes);
  132. }
  133. }
  134. }