using System; using System.Collections.Generic; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace HaBookCms.Common.HttpHelper { public class HttpHelper { /// /// 同步GET请求 /// /// /// /// 请求响应超时时间,单位/s(默认100秒) /// public static string HttpGet(string url, Dictionary headers = null, int timeout = 0) { using (HttpClient client = new HttpClient()) { if (headers != null) { foreach (KeyValuePair header in headers) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } if (timeout > 0) { client.Timeout = new TimeSpan(0, 0, timeout); } Byte[] resultBytes = client.GetByteArrayAsync(url).Result; return Encoding.UTF8.GetString(resultBytes); } } /// /// 异步GET请求 /// /// /// /// 请求响应超时时间,单位/s(默认100秒) /// public static async Task HttpGetAsync(string url, Dictionary headers = null, int timeout = 0) { using (HttpClient client = new HttpClient()) { if (headers != null) { foreach (KeyValuePair header in headers) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } if (timeout > 0) { client.Timeout = new TimeSpan(0, 0, timeout); } Byte[] resultBytes = await client.GetByteArrayAsync(url); return Encoding.Default.GetString(resultBytes); } } /// /// 同步POST请求 /// /// /// /// /// /// 请求响应超时时间,单位/s(默认100秒) /// 默认UTF8 /// public static string HttpPost(string url, string postData, Dictionary headers = null, string contentType = null, int timeout = 0, Encoding encoding = null) { using (HttpClient client = new HttpClient()) { if (headers != null) { foreach (KeyValuePair header in headers) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } if (timeout > 0) { client.Timeout = new TimeSpan(0, 0, timeout); } using (HttpContent content = new StringContent(postData ?? "", encoding ?? Encoding.UTF8)) { if (contentType != null) { content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); } using (HttpResponseMessage responseMessage = client.PostAsync(url, content).Result) { Byte[] resultBytes = responseMessage.Content.ReadAsByteArrayAsync().Result; return Encoding.UTF8.GetString(resultBytes); } } } } /// /// 异步POST请求 /// /// /// /// /// /// 请求响应超时时间,单位/s(默认100秒) /// 默认UTF8 /// public static async Task HttpPostAsync(string url, string postData, Dictionary headers = null, string contentType = null, int timeout = 0, Encoding encoding = null) { using (HttpClient client = new HttpClient()) { if (headers != null) { foreach (KeyValuePair header in headers) { client.DefaultRequestHeaders.Add(header.Key, header.Value); } } if (timeout > 0) { client.Timeout = new TimeSpan(0, 0, timeout); } using (HttpContent content = new StringContent(postData ?? "", encoding ?? Encoding.UTF8)) { if (contentType != null) { content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType); } using (HttpResponseMessage responseMessage = await client.PostAsync(url, content)) { Byte[] resultBytes = await responseMessage.Content.ReadAsByteArrayAsync(); return Encoding.UTF8.GetString(resultBytes); } } } } } }