using System; using System.IO; using System.Net; using System.Text; using System.Web; namespace BatchService.Framework.Utility { public class HttpHelper { public static string PostHttp(string url, string body) { return PostHttp(url, body, "application/x-www-form-urlencoded"); } //body是要传递的参数,格式"roleId=1&uid=2" //post的cotentType填写: //"application/x-www-form-urlencoded" //soap填写:"text/xml; charset=utf-8" public static string PostHttp(string url, string body, string contentType, string token = "") { LogHelper.Debug(url); HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url); httpWebRequest.ContentType = contentType; httpWebRequest.Method = "POST"; DateTime date1 = DateTime.Now; httpWebRequest.Timeout = 20000; byte[] btBodys = Encoding.UTF8.GetBytes(body); httpWebRequest.ContentLength = btBodys.Length; httpWebRequest.Headers.Add("Token", token); Stream newStream = httpWebRequest.GetRequestStream(); newStream.Write(btBodys, 0, btBodys.Length); newStream.Close(); HttpWebResponse httpWebResponse; DateTime date2 = DateTime.Now; try { httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); } catch (WebException ex) { LogHelper.Error(ex.ToString()); httpWebResponse = (HttpWebResponse)ex.Response; StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream()); var strHtml = sr.ReadToEnd(); LogHelper.Error(strHtml); throw ex; } StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()); string responseContent = streamReader.ReadToEnd(); httpWebResponse.Close(); streamReader.Close(); httpWebRequest.Abort(); httpWebResponse.Close(); return responseContent; } public static string GetHttp(string url, HttpContext httpContext) { string queryString = "?"; //foreach (string key in httpContext.Request.QueryString.AllKeys) //{ // queryString += key + "=" + httpContext.Request.QueryString[key] + "&"; //} queryString = queryString.Substring(0, queryString.Length - 1); HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url + queryString); //httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = "GET"; httpWebRequest.Timeout = 20000; httpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8"; string responseContent = ""; using (WebResponse wr = httpWebRequest.GetResponse()) { Stream stream = wr.GetResponseStream(); byte[] buf = new byte[1024]; while (true) { int len = stream.Read(buf, 0, buf.Length); if (len <= 0)//[2011-8-22] 修改len < 0 =》 len <= 0 解决死循环 break; responseContent += Encoding.UTF8.GetString(buf, 0, len); } } //HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); //StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()); //string responseContent = streamReader.ReadToEnd(); //httpWebResponse.Close(); //streamReader.Close(); return responseContent; } public static MemoryStream GetImage(string url) { HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url); myRequest.Method = "GET"; MemoryStream ms = null; HttpWebResponse myResponse = null; try { myResponse = (HttpWebResponse)myRequest.GetResponse(); StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); Stream stream = myResponse.GetResponseStream(); #region 保存下载图片 Byte[] buffer = new Byte[myResponse.ContentLength]; int offset = 0, actuallyRead = 0; do { actuallyRead = stream.Read(buffer, offset, buffer.Length - offset); offset += actuallyRead; } while (actuallyRead > 0); ms = new MemoryStream(buffer); byte[] buffurPic = ms.ToArray(); //System.IO.File.WriteAllBytes(pathName, buffurPic); #endregion myResponse.Close(); stream.Close(); //fileStream.Close(); } //异常请求 catch (WebException ex) { LogHelper.Error(ex.ToString()); } return ms; } public static string GetIPAddress() { if (HttpContext.Current == null) { return ""; } string userHostAddress = HttpContext.Current.Request.UserHostAddress; if (string.IsNullOrEmpty(userHostAddress)) { userHostAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } //最后判断获取是否成功,并检查IP地址的格式(检查其格式非常重要) if (!string.IsNullOrEmpty(userHostAddress) && IsIP(userHostAddress)) { return userHostAddress; } return "127.0.0.1"; } /// /// 检查IP地址格式 /// /// /// public static bool IsIP(string ip) { return System.Text.RegularExpressions.Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"); } } }