HttpHelper.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Http;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace IoTIntegrationPlatform.Common
  8. {
  9. /// <summary>
  10. /// http公共类
  11. /// </summary>
  12. public static class HttpHelper
  13. {
  14. /// <summary>
  15. /// post方法异步请求
  16. /// </summary>
  17. /// <param name="url">目标地址</param>
  18. /// <param name="json">发送的参数字符串</param>
  19. /// <returns></returns>
  20. public static async Task<string> PostAsync(string url, string json)
  21. {
  22. string responseBody = string.Empty;
  23. try
  24. {
  25. HttpClient client = new HttpClient(new HttpClientHandler() { UseCookies = false });
  26. var data = new StringContent(json, Encoding.UTF8, "application/json");
  27. HttpResponseMessage response = await client.PostAsync(url, data);
  28. response.EnsureSuccessStatusCode();
  29. responseBody = await response.Content.ReadAsStringAsync();
  30. }
  31. catch (Exception ex)
  32. {
  33. Logging.Error(ex, ex.Message);
  34. }
  35. return responseBody;
  36. }
  37. }
  38. }