using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Text; namespace Znyc.Recruitment.Admin.Commons.Helpers { /// /// http请求类 /// public static class HttpRequestHelper { /// /// /// /// public static void FillFormDataStream(this Dictionary formData, Stream stream) { string dataString = GetQueryString(formData); byte[] formDataBytes = formData == null ? new byte[0] : Encoding.UTF8.GetBytes(dataString); stream.Write(formDataBytes, 0, formDataBytes.Length); stream.Seek(0, SeekOrigin.Begin); //设置指针读取位置 } /// /// 组装QueryString的方法 /// 参数之间用&连接,首位没有符号,如:a=1&b=2&c=3 /// /// /// public static string GetQueryString(this Dictionary formData) { if (formData == null || formData.Count == 0) { return ""; } StringBuilder sb = new StringBuilder(); int i = 0; foreach (KeyValuePair kv in formData) { i++; sb.AppendFormat("{0}={1}", kv.Key, kv.Value); if (i < formData.Count) { sb.Append("&"); } } return sb.ToString(); } #region 同步方法 /// /// 使用Get方法获取字符串结果 /// /// /// /// /// public static string HttpGet(string url, Encoding encoding = null, int timeOut = 60000) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.Timeout = timeOut; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (Stream responseStream = response.GetResponseStream()) { using (StreamReader myStreamReader = new StreamReader(responseStream, encoding ?? Encoding.GetEncoding("utf-8"))) { string retString = myStreamReader.ReadToEnd(); return retString; } } } /// /// 使用Post方法获取字符串结果,常规提交 /// /// public static string HttpPost(string url, Dictionary formData = null, Encoding encoding = null, int timeOut = 60000, Dictionary headers = null) { MemoryStream ms = new MemoryStream(); formData.FillFormDataStream(ms); //填充formData return HttpPost(url, ms, "application/x-www-form-urlencoded", encoding, headers, timeOut); } /// /// 发送HttpPost请求,使用JSON格式传输数据 /// /// /// /// /// /// public static string HttpPost(string url, string postData, Encoding encoding = null, Dictionary headers = null) { if (encoding == null) { encoding = Encoding.UTF8; } if (string.IsNullOrWhiteSpace(postData)) { throw new ArgumentNullException("postData"); } byte[] data = encoding.GetBytes(postData); MemoryStream stream = new MemoryStream(); byte[] formDataBytes = postData == null ? new byte[0] : Encoding.UTF8.GetBytes(postData); stream.Write(formDataBytes, 0, formDataBytes.Length); stream.Seek(0, SeekOrigin.Begin); //设置指针读取位置 return HttpPost(url, stream, "application/json", encoding, headers); } /// /// 使用POST请求数据,使用JSON传输数据 /// /// /// 传输对象,转换为JSON传输 /// /// public static string HttpPost(string url, object dataObj, Encoding encoding = null, Dictionary headers = null) { if (encoding == null) { encoding = Encoding.UTF8; } if (dataObj == null) { throw new ArgumentNullException("dataObj"); } string postData = JsonConvert.SerializeObject(dataObj, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" }); byte[] data = encoding.GetBytes(postData); MemoryStream stream = new MemoryStream(); byte[] formDataBytes = postData == null ? new byte[0] : Encoding.UTF8.GetBytes(postData); stream.Write(formDataBytes, 0, formDataBytes.Length); stream.Seek(0, SeekOrigin.Begin); //设置指针读取位置 return HttpPost(url, stream, "application/json", encoding, headers); } /// /// 使用Post方法获取字符串结果 /// /// /// /// /// /// /// /// public static string HttpPost(string url, Stream postStream = null, string contentType = "application/x-www-form-urlencoded", Encoding encoding = null, Dictionary headers = null, int timeOut = 60000) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Timeout = timeOut; request.ContentLength = postStream != null ? postStream.Length : 0; request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; request.KeepAlive = false; request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.87 Safari/537.36 QQBrowser/9.2.5748.400"; request.ContentType = contentType; if (headers != null) { foreach (KeyValuePair header in headers) { request.Headers.Add(header.Key, header.Value); } } #region 输入二进制流 if (postStream != null) { postStream.Position = 0; //直接写入流 Stream requestStream = request.GetRequestStream(); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = postStream.Read(buffer, 0, buffer.Length)) != 0) { requestStream.Write(buffer, 0, bytesRead); } postStream.Close(); //关闭文件访问 } #endregion 输入二进制流 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (Stream responseStream = response.GetResponseStream()) { using (StreamReader myStreamReader = new StreamReader(responseStream, encoding ?? Encoding.GetEncoding("utf-8"))) { string retString = myStreamReader.ReadToEnd(); return retString; } } } #endregion 同步方法 } }