1 public class HttpHelper
2 {
3 /// <summary>
4 /// 发起POST同步请求
5 /// </summary>
6 /// <param name="url"></param>
7 /// <param name="postData"></param>
8 /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
9 /// <param name="headers">填充消息头</param>
10 /// <returns></returns>
11 public static string HttpPost(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
12 {
13 postData ??= "";
14 using HttpClient client = new HttpClient();
15 client.Timeout = new TimeSpan(0, 0, timeOut);
16 if (headers != null)
17 {
18 foreach (var header in headers)
19 client.DefaultRequestHeaders.Add(header.Key, header.Value);
20 }
21 using HttpContent httpContent = new StringContent(postData, Encoding.UTF8);
22 if (contentType != null)
23 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
24
25 HttpResponseMessage response = client.PostAsync(url, httpContent).Result;
26 return response.Content.ReadAsStringAsync().Result;
27 }
28
29 /// <summary>
30 /// 发起POST异步请求
31 /// </summary>
32 /// <param name="url"></param>
33 /// <param name="postData"></param>
34 /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
35 /// <param name="headers">填充消息头</param>
36 /// <returns></returns>
37 public static async Task<string> HttpPostAsync(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
38 {
39 postData ??= "";
40 using HttpClient client = new HttpClient();
41 client.Timeout = new TimeSpan(0, 0, timeOut);
42 if (headers != null)
43 {
44 foreach (var header in headers)
45 client.DefaultRequestHeaders.Add(header.Key, header.Value);
46 }
47 using HttpContent httpContent = new StringContent(postData, Encoding.UTF8);
48 if (contentType != null)
49 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
50
51 HttpResponseMessage response = await client.PostAsync(url, httpContent);
52 return await response.Content.ReadAsStringAsync();
53 }
54
55 /// <summary>
56 /// 发起GET同步请求
57 /// </summary>
58 /// <param name="url"></param>
59 /// <param name="headers"></param>
60 /// <param name="contentType"></param>
61 /// <returns></returns>
62 public static string HttpGet(string url, Dictionary<string, string> headers = null)
63 {
64 using HttpClient client = new HttpClient();
65 if (headers != null)
66 {
67 foreach (var header in headers)
68 client.DefaultRequestHeaders.Add(header.Key, header.Value);
69 }
70 else
71 {
72 client.DefaultRequestHeaders.Add("ContentType", "application/x-www-form-urlencoded");
73 client.DefaultRequestHeaders.Add("UserAgent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
74 }
75 try
76 {
77 HttpResponseMessage response = client.GetAsync(url).Result;
78 return response.Content.ReadAsStringAsync().Result;
79 }
80 catch (Exception ex)
81 {
82 //TODO 打印日志
83 Console.WriteLine($"[Http请求出错]{url}|{ex.Message}");
84 }
85 return "";
86 }
87
88 /// <summary>
89 /// 发起GET异步请求
90 /// </summary>
91 /// <param name="url"></param>
92 /// <param name="headers"></param>
93 /// <returns></returns>
94 public static async Task<string> HttpGetAsync(string url, Dictionary<string, string> headers = null)
95 {
96 using HttpClient client = new HttpClient();
97 if (headers != null)
98 {
99 foreach (var header in headers)
100 client.DefaultRequestHeaders.Add(header.Key, header.Value);
101 }
102 HttpResponseMessage response = await client.GetAsync(url);
103 return await response.Content.ReadAsStringAsync();
104 }
105
106 /// <summary>
107 /// 发起Put同步请求
108 /// </summary>
109 /// <param name="url"></param>
110 /// <param name="postData"></param>
111 /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
112 /// <param name="headers">填充消息头</param>
113 /// <returns></returns>
114 public static string HttpPut(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
115 {
116 postData ??= "";
117 using HttpClient client = new HttpClient();
118 client.Timeout = new TimeSpan(0, 0, timeOut);
119 if (headers != null)
120 {
121 foreach (var header in headers)
122 client.DefaultRequestHeaders.Add(header.Key, header.Value);
123 }
124 using HttpContent httpContent = new StringContent(postData, Encoding.UTF8);
125 if (contentType != null)
126 httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
127
128 HttpResponseMessage response = client.PutAsync(url, httpContent).Result;
129 return response.Content.ReadAsStringAsync().Result;
130 }
131 }