12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Text;
- using System.Threading.Tasks;
- using Newtonsoft.Json;
- namespace WebAPIBase.Utils
- {
- public class HttpClientHelper
- {
- /// <summary>
- /// HttpClient实现Post请求
- /// </summary>
- public static string DoPost(string url)
- {
- string result = "";
- var obj = new UserLoginInfo
- {
- Cpu = Computer.CpuID,
- MotherBoard = Computer.MotherboardID,
- PhysicalMemory = Computer.TotalPhysicalMemory,
- Ip = RequestHelper.GetServerIp()
- };
- using (HttpClient httpClient = new HttpClient())
- {
- //post方式
- //参数
- var content = new StringContent(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json");
- HttpResponseMessage response = httpClient.PostAsync(url, content).Result;
- //确保Http响应成功
- if (response.IsSuccessStatusCode)
- {
- result = response.Content.ReadAsStringAsync().Result;
- }
- }
- return result;
- }
- }
- /// <summary>
- /// 传入的主板,cpu,内存等信息
- /// </summary>
- public class UserLoginInfo
- {
- [JsonProperty(PropertyName = "Cpu", NullValueHandling = NullValueHandling.Ignore)]
- public string Cpu { get; set; }
- [JsonProperty(PropertyName = "MotherBoard", NullValueHandling = NullValueHandling.Ignore)]
- public string MotherBoard { get; set; }
- [JsonProperty(PropertyName = "PhysicalMemory", NullValueHandling = NullValueHandling.Ignore)]
- public string PhysicalMemory { get; set; }
- public string Ip { get; set; }
- }
- }
|