using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Security.Cryptography; using System.Text; using System.Web; namespace BLV_API { public static class Tools { /// /// 获取IP /// /// /// public static string GetClientIP(HttpRequestBase Request) { string IP = ""; string PPP =Request.UserHostAddress; string III =Request.ServerVariables["REMOTE_ADDR"]; if (string.IsNullOrEmpty(III)) { IP = PPP; } else { IP = III; } return IP; } public static string GetApplicationPath() { return AppDomain.CurrentDomain.SetupInformation.ApplicationBase; } public static string MD5Encrypt(string strEnc) { string result; try { MD5 md5Hasher = MD5.Create(); byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(strEnc)); StringBuilder sBuilder = new StringBuilder(); int num; for (int i = 0; i < data.Length; i = num + 1) { sBuilder.Append(data[i].ToString("x2")); num = i; } result = sBuilder.ToString(); } catch (Exception ex) { throw ex; } return result; } /// /// post数据接口 /// /// /// /// post和put,默认post /// public static string PostWebRequest(string url, string postData) { string result = string.Empty; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "POST"; //req.Timeout = 10000;//设置请求超时时间,单位为毫秒 req.ContentType = "application/json"; byte[] data = Encoding.UTF8.GetBytes(postData); req.ContentLength = data.Length; using (Stream reqStream = req.GetRequestStream()) { reqStream.Write(data, 0, data.Length); } HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); using (Stream stream = resp.GetResponseStream()) { using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd();//获取响应内容 } } return result; } /// /// post数据接口 /// /// /// /// /// public static string PostWebRequest(string url, string headerAuthorization, string postData) { string result = string.Empty; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; req.Headers.Add("Authorization", headerAuthorization); byte[] data = Encoding.UTF8.GetBytes(postData); req.ContentLength = data.Length; using (Stream reqStream = req.GetRequestStream()) { reqStream.Write(data, 0, data.Length); } HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); using (Stream stream = resp.GetResponseStream()) { using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd();//获取响应内容 } } return result; } } }