HttpHelper.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.Text;
  8. using Microsoft.Extensions.Primitives;
  9. using Newtonsoft.Json;
  10. using Newtonsoft.Json.Linq;
  11. namespace ET.Server
  12. {
  13. public static class HttpHelper
  14. {
  15. public static async ETTask<string> GetBodyParameter(HttpListenerContext context)
  16. {
  17. return await new StreamReader(context.Request.InputStream).ReadToEndAsync();
  18. }
  19. public static void Response(HttpListenerContext context, object response)
  20. {
  21. byte[] bytes = JsonHelper.ToJson(response).ToUtf8();
  22. context.Response.StatusCode = 200;
  23. context.Response.ContentEncoding = Encoding.UTF8;
  24. context.Response.ContentLength64 = bytes.Length;
  25. context.Response.OutputStream.Write(bytes, 0, bytes.Length);
  26. }
  27. /// <summary>
  28. /// post请求抖音接口
  29. /// </summary>
  30. /// <param name="url"></param>
  31. /// <param name="heads"></param>
  32. /// <param name="param"></param>
  33. /// <returns></returns>
  34. public static string PostRequestByDouyin(string url, Dictionary<string, string> heads, JObject param)
  35. {
  36. if (string.IsNullOrEmpty(url) || param is not { Count: > 0 })
  37. {
  38. return null;
  39. }
  40. using HttpClient httpClient = new HttpClient();
  41. httpClient.DefaultRequestHeaders.Accept.TryParseAdd("application/json");
  42. // 设置请求头
  43. if (heads is { Count: >= 0 })
  44. {
  45. foreach (var head in heads)
  46. {
  47. httpClient.DefaultRequestHeaders.Add(head.Key, head.Value);
  48. }
  49. }
  50. // post
  51. HttpContent content = new StringContent(JsonConvert.SerializeObject(param), Encoding.UTF8, "application/json");
  52. // response
  53. HttpResponseMessage response = httpClient.PostAsync(new Uri(url), content).Result;
  54. if (response.IsSuccessStatusCode)
  55. {
  56. return response.Content.ReadAsStringAsync().Result;
  57. }
  58. string statusCode = response.StatusCode.ToString();
  59. Log.Debug($"http请求错误...statusCode:{statusCode}, url:{url}");
  60. return null;
  61. }
  62. /// <summary>
  63. /// get请求抖音接口
  64. /// </summary>
  65. /// <param name="baseUrl"></param>
  66. /// <param name="heads"></param>
  67. /// <param name="param"></param>
  68. /// <returns></returns>
  69. public static string GetRequestByDouyin(string baseUrl, Dictionary<string, string> heads, Dictionary<string, string> param)
  70. {
  71. if (string.IsNullOrEmpty(baseUrl) || param is not { Count: > 0 })
  72. {
  73. return null;
  74. }
  75. using HttpClient httpClient = new HttpClient();
  76. httpClient.DefaultRequestHeaders.Accept.TryParseAdd("application/json");
  77. // 设置请求头
  78. if (heads is { Count: >= 0 })
  79. {
  80. foreach (var head in heads)
  81. {
  82. httpClient.DefaultRequestHeaders.Add(head.Key, head.Value);
  83. }
  84. }
  85. StringBuilder url = new StringBuilder(baseUrl);
  86. if (param is { Count: >= 0 })
  87. {
  88. url.Append('?');
  89. List<string> keyList = new List<string>(param.Keys);
  90. for (int i = 0; i < param.Count; i++)
  91. {
  92. url.Append(keyList[i]).Append('=').Append(param[keyList[i]]);
  93. if (i < param.Count)
  94. {
  95. url.Append('&');
  96. }
  97. }
  98. }
  99. HttpResponseMessage response = httpClient.GetAsync(new Uri(url.ToString())).Result;
  100. if (response.IsSuccessStatusCode)
  101. {
  102. return response.Content.ReadAsStringAsync().Result;
  103. }
  104. string statusCode = response.StatusCode.ToString();
  105. Log.Debug($"http请求错误...statusCode:{statusCode}, url:{url}");
  106. return null;
  107. }
  108. }
  109. }