using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace RoadFlow.Utility { 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); } } } } /// /// 上传文件 /// /// /// /// public static string SendFile(string url, string path, byte[] bf) { HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; CookieContainer cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; request.AllowAutoRedirect = true; request.Method = "POST"; string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线 request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary; byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n"); byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); //int pos = path.LastIndexOf("\\"); string fileName = Path.GetFileName(path); //请求头部信息 StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"media\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName)); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString()); Stream postStream = request.GetRequestStream(); postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length); postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); postStream.Write(bf, 0, bf.Length); postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); postStream.Close(); //发送请求并获取相应回应数据 HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream instream = response.GetResponseStream(); StreamReader sr = new StreamReader(instream, Encoding.UTF8); string content = sr.ReadToEnd(); return content; } } }