using Newtonsoft.Json; using System; using System.Web; using System.IO; using System.Net; using System.Text; namespace iWareCommon.Utils { public class HttpHelper { /// /// 获取response的静态方法 /// /// 请求地址 /// 请求参数 /// 延迟时间 /// 请求的response public static string GetHttpResponse(string url, object postData, string token, int timeout) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/json;charset=UTF-8"; request.UserAgent = null; request.Timeout = timeout; request.Headers.Add("Authorization", token); var myRequestStream = request.GetRequestStream(); var json = postData == null ? "" : JsonConvert.SerializeObject(postData); var jsonBytes = UTF8Encoding.UTF8.GetBytes(json); myRequestStream.Write(jsonBytes, 0, jsonBytes.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; } /// /// 获取response的静态方法 /// /// 请求地址 /// 请求参数 /// 延迟时间 /// 请求的response public static string GetHttpResponse(string url, object postData,int timeout) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/json;charset=UTF-8"; request.UserAgent = null; request.Timeout = timeout; request.Headers.Add("Authorization", "eyJhbGciOiJIUzM4NCIsInR5cCI6IkpXVCJ9.eyJpYXQiOiIxNTUzNTMwNjAwIiwiZXhwIjoiMTYxNjYwMjYwMCIsImlzcyI6IlNBR1dXSVAiLCJzdWIiOiJ3Y3NpbnRlZ3JhdGVkIiwidXNlcm5hbWUiOiJ3Y3NpbnRlZ3JhdGVkIiwidXNlcmdyb3VwIjpbeyJsb2dpbm5hbWUiOiJ3Y3NpbnRlZ3JhdGVkIiwiZ3JvdXBJZCI6Ijg3YjE1MTQ1LWUwODMtNDM2Ny04ZjdiLTNiNTI3NWJhYzExZSIsImdyb3VwbmFtZSI6IlJDU2ludGVncmF0ZWQiLCJyb2xlSWQiOiI4NjNhMGIxZS0zODAzLTQyNmMtOTRlNC04OTRmNTE1ZWQyYzgiLCJyb2xlbmFtZSI6IlJDU-mbhuaIkCJ9LHsibG9naW5uYW1lIjoid2NzaW50ZWdyYXRlZCIsImdyb3VwSWQiOiI5ZGU3ZjA5Zi05YzhiLTQwNDEtODc0NS1hYjgxNGQ0OTFiODEiLCJncm91cG5hbWUiOiJXQ1NpbnRlZ3JhdGVkIiwicm9sZUlkIjoiNWJhZDE1YTUtOTQ1NC00YTc3LTk0OGYtNzUxMjI2NDdiOTA4Iiwicm9sZW5hbWUiOiJXQ1Ppm4bmiJAifV19.AN9iUPMFZ5w7UW7D5kERV903gPCtyk6o2STZILX7QzkVjKLLSWgpUnQYBBfiSSQk"); var myRequestStream = request.GetRequestStream(); var json = postData == null ? "" : JsonConvert.SerializeObject(postData); var jsonBytes = UTF8Encoding.UTF8.GetBytes(json); myRequestStream.Write(jsonBytes,0,jsonBytes.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; } /// /// 获取response的静态方法 /// /// 请求地址 /// 请求参数 /// 延迟时间 /// 请求的response public static T2 GetHttpResponse(string url, T postData, int timeout) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/json;charset=UTF-8"; request.UserAgent = null; request.Timeout = timeout; //request.Headers.Add("Authorization", "eyJhbGciOiJIUzM4NCIsInR5cCI6IkpXVCJ9.eyJpYXQiOiIxNTUzNTMwNjAwIiwiZXhwIjoiMTYxNjYwMjYwMCIsImlzcyI6IlNBR1dXSVAiLCJzdWIiOiJ3Y3NpbnRlZ3JhdGVkIiwidXNlcm5hbWUiOiJ3Y3NpbnRlZ3JhdGVkIiwidXNlcmdyb3VwIjpbeyJsb2dpbm5hbWUiOiJ3Y3NpbnRlZ3JhdGVkIiwiZ3JvdXBJZCI6Ijg3YjE1MTQ1LWUwODMtNDM2Ny04ZjdiLTNiNTI3NWJhYzExZSIsImdyb3VwbmFtZSI6IlJDU2ludGVncmF0ZWQiLCJyb2xlSWQiOiI4NjNhMGIxZS0zODAzLTQyNmMtOTRlNC04OTRmNTE1ZWQyYzgiLCJyb2xlbmFtZSI6IlJDU-mbhuaIkCJ9LHsibG9naW5uYW1lIjoid2NzaW50ZWdyYXRlZCIsImdyb3VwSWQiOiI5ZGU3ZjA5Zi05YzhiLTQwNDEtODc0NS1hYjgxNGQ0OTFiODEiLCJncm91cG5hbWUiOiJXQ1NpbnRlZ3JhdGVkIiwicm9sZUlkIjoiNWJhZDE1YTUtOTQ1NC00YTc3LTk0OGYtNzUxMjI2NDdiOTA4Iiwicm9sZW5hbWUiOiJXQ1Ppm4bmiJAifV19.AN9iUPMFZ5w7UW7D5kERV903gPCtyk6o2STZILX7QzkVjKLLSWgpUnQYBBfiSSQk"); var myRequestStream = request.GetRequestStream(); var json = postData == null ? "" : JsonConvert.SerializeObject(postData); var jsonBytes = UTF8Encoding.UTF8.GetBytes(json); myRequestStream.Write(jsonBytes, 0, jsonBytes.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); T2 result = JsonConvert.DeserializeObject(retString); return result; } /// /// Http上传文件 /// /// 上传的路径 /// 本地文件路径 /// 反馈的信息 public static string HttpUploadFile(string url, string path) { // 设置参数 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.Substring(pos + 1); //请求头部信息 StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName)); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString()); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); byte[] bArr = new byte[fs.Length]; fs.Read(bArr, 0, bArr.Length); fs.Close(); Stream postStream = request.GetRequestStream(); postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length); postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); postStream.Write(bArr, 0, bArr.Length); postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); postStream.Close(); //发送请求并获取相应回应数据 HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream instream = response.GetResponseStream(); StreamReader sr = new StreamReader(instream, Encoding.UTF8); //返回结果网页(html)代码 string content = sr.ReadToEnd(); return content; } /// /// Http上传文件 /// /// 上传的路径 /// 本地文件路径 /// 反馈的信息 public static string HttpUploadFile(string url, object postData, string token, int timeout, string path) { // 设置参数 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; request.Timeout = timeout; request.Headers.Add("Authorization", token); var myRequestStream = request.GetRequestStream(); var json = postData == null ? "" : JsonConvert.SerializeObject(postData); var jsonBytes = UTF8Encoding.UTF8.GetBytes(json); myRequestStream.Write(jsonBytes, 0, jsonBytes.Length); 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.Substring(pos + 1); //请求头部信息 StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", fileName)); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString()); FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); byte[] bArr = new byte[fs.Length]; fs.Read(bArr, 0, bArr.Length); fs.Close(); Stream postStream = request.GetRequestStream(); postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length); postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); postStream.Write(bArr, 0, bArr.Length); postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); postStream.Close(); //发送请求并获取相应回应数据 HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream instream = response.GetResponseStream(); StreamReader sr = new StreamReader(instream, Encoding.UTF8); //返回结果网页(html)代码 string content = sr.ReadToEnd(); return content; } /// /// Http上传文件 /// /// 上传的路径 /// 本地文件路径 /// 反馈的信息 public static string HttpUploadFile(string url, HttpPostedFile file, string token, int timeout) { // 设置参数 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; request.Timeout = timeout; request.Headers.Add("Authorization", token); byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "\r\n"); byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); //请求头部信息 StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name=\"file\";filename=\"{0}\"\r\nContent-Type:application/octet-stream\r\n\r\n", file.FileName)); byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString()); int fileLen = file.ContentLength; byte[] bArray = new byte[fileLen]; file.InputStream.Read(bArray, 0, fileLen); Stream postStream = request.GetRequestStream(); postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length); postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length); postStream.Write(bArray, 0, bArray.Length); postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); postStream.Close(); //发送请求并获取相应回应数据 HttpWebResponse response = request.GetResponse() as HttpWebResponse; //直到request.GetResponse()程序才开始向目标网页发送Post请求 Stream instream = response.GetResponseStream(); StreamReader sr = new StreamReader(instream, Encoding.UTF8); //返回结果网页(html)代码 string content = sr.ReadToEnd(); return content; } } }