HttpClientHelper.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Net.Http.Headers;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Newtonsoft.Json;
  10. namespace WebAPIBase.Utils
  11. {
  12. public class HttpClientHelper
  13. {
  14. /// <summary>
  15. /// HttpClient实现Post请求
  16. /// </summary>
  17. public static string DoPost(string url)
  18. {
  19. string result = "";
  20. var obj = new UserLoginInfo
  21. {
  22. Cpu = Computer.CpuID,
  23. MotherBoard = Computer.MotherboardID,
  24. PhysicalMemory = Computer.TotalPhysicalMemory,
  25. Ip = RequestHelper.GetServerIp()
  26. };
  27. using (HttpClient httpClient = new HttpClient())
  28. {
  29. //post方式
  30. //参数
  31. var content = new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json");
  32. HttpResponseMessage response = httpClient.PostAsync(url, content).Result;
  33. //确保Http响应成功
  34. if (response.IsSuccessStatusCode)
  35. {
  36. result = response.Content.ReadAsStringAsync().Result;
  37. }
  38. }
  39. return result;
  40. }
  41. }
  42. /// <summary>
  43. /// 传入的主板,cpu,内存等信息
  44. /// </summary>
  45. public class UserLoginInfo
  46. {
  47. [JsonProperty(PropertyName = "Cpu", NullValueHandling = NullValueHandling.Ignore)]
  48. public string Cpu { get; set; }
  49. [JsonProperty(PropertyName = "MotherBoard", NullValueHandling = NullValueHandling.Ignore)]
  50. public string MotherBoard { get; set; }
  51. [JsonProperty(PropertyName = "PhysicalMemory", NullValueHandling = NullValueHandling.Ignore)]
  52. public string PhysicalMemory { get; set; }
  53. public string Ip { get; set; }
  54. }
  55. }