using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; using System.Web; using System.Net; using System.IO; namespace LYFZ.Weixin.SDK.Helpers { public class Util { /// /// Sha1 /// /// /// /// public static string Sha1(string orgStr, string encode = "UTF-8") { var sha1 = new SHA1Managed(); var sha1bytes = System.Text.Encoding.GetEncoding(encode).GetBytes(orgStr); byte[] resultHash = sha1.ComputeHash(sha1bytes); string sha1String = BitConverter.ToString(resultHash).ToLower(); sha1String = sha1String.Replace("-", ""); return sha1String; } public static string MD5(string encypStr, string charset = "UTF-8") { string retStr; MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider(); //创建md5对象 byte[] inputBye; byte[] outputBye; //使用GB2312编码方式把字符串转化为字节数组. try { inputBye = Encoding.GetEncoding(charset).GetBytes(encypStr); } catch { inputBye = Encoding.GetEncoding("GB2312").GetBytes(encypStr); } outputBye = m5.ComputeHash(inputBye); retStr = System.BitConverter.ToString(outputBye); retStr = retStr.Replace("-", "").ToUpper(); return retStr; } private static string[] strs = new string[] { "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z", "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z" }; /// /// 创建随机字符串 /// /// public static string CreateNonce_str() { Random r = new Random(); var sb = new StringBuilder(); var length = strs.Length; for (int i = 0; i < 15; i++) { sb.Append(strs[r.Next(length - 1)]); } return sb.ToString(); } /// /// 创建时间戳 /// /// public static long CreateTimestamp() { return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000; } /// /// html转义 /// /// /// public static string Transfer(string instr) { if (instr == null) return ""; return instr.Replace("&", "&").Replace("<", "<") .Replace(">", ">").Replace("\"", """); } /// /// html转义字符串还原 /// /// /// public static string DeTransfer(string instr) { if (instr == null) return ""; return instr.Replace("&", "&").Replace("<", "<") .Replace(">", ">").Replace(""", "\""); } /// /// FORM表单POST方式上传一个多媒体文件 /// /// API URL /// /// /// /// /// public static string HttpRequestPost(string url, string typeName, string fileName, Stream fs, string encoding = "UTF-8") { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Timeout = 10000; var postStream = new MemoryStream(); #region 处理Form表单文件上传 //通过表单上传文件 string boundary = "----" + DateTime.Now.Ticks.ToString("x"); string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n"; try { var formdata = string.Format(formdataTemplate, typeName, fileName); var formdataBytes = Encoding.ASCII.GetBytes(postStream.Length == 0 ? formdata.Substring(2, formdata.Length - 2) : formdata);//第一行不需要换行 postStream.Write(formdataBytes, 0, formdataBytes.Length); //写入文件 byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) != 0) { postStream.Write(buffer, 0, bytesRead); } } catch (Exception ex) { throw ex; } //结尾 var footer = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); postStream.Write(footer, 0, footer.Length); request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary); #endregion 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 = true; request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36"; #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.GetEncoding(encoding))) { string retString = myStreamReader.ReadToEnd(); return retString; } } } /// /// 下载 /// /// /// public static void Download(string url, Stream stream) { WebClient wc = new WebClient(); var data = wc.DownloadData(url); foreach (var b in data) { stream.WriteByte(b); } } } }