当前位置 : 主页 > 编程语言 > c语言 >

本人亲测-C#常用工具类

来源:互联网 收集:自由互联 发布时间:2021-06-25
/* * HTTP 接口工具类 */ public class HttpUitls { /* * get 请求 */ public static string Get(string Url) { //System.GC.Collect(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Proxy = null; request.KeepAlive



/* * HTTP接口工具类 */ public class HttpUitls { /* * get请求 */ public static string Get(string Url) { //System.GC.Collect(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Proxy = null; request.KeepAlive = false; request.Method = "GET"; request.ContentType = "application/json; charset=UTF-8"; request.AutomaticDecompression = DecompressionMethods.GZip; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); if (response != null) { response.Close(); } if (request != null) { request.Abort(); } return retString; } /* * post请求 Referer(允许为空) */ public static string Post(string Url, string Data, string Referer) { string retString = ""; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Method = "POST"; request.Referer = Referer; byte[] bytes = Encoding.UTF8.GetBytes(Data); request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = bytes.Length; Stream myResponseStream = request.GetRequestStream(); myResponseStream.Write(bytes, 0, bytes.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); if (response != null) { response.Close(); } if (request != null) { request.Abort(); } } catch (Exception ex) { Console.WriteLine("异常:" + ex); } return retString; } }



/* * Wav音频播放工具类 */ public class WavUtils { //播放 public static void WAVPlay(string myWav) { SoundPlayer sound = new SoundPlayer(); sound.SoundLocation = @myWav; sound.Load(); sound.Play(); } //暂停 public static void WAVStop(string myWav) { SoundPlayer sound = new SoundPlayer(); sound.SoundLocation = @myWav; sound.Load(); sound.Stop(); } }



/* * 加密解密工具类 */ public class EncryptHelper { //默认密钥 private static string AESKey = "[45/*YUIdse..e;]"; private static string DESKey = "[&HdN72]"; /// <summary> /// AES加密 /// </summary> public static string AESEncrypt(string value, string _aeskey = null) { if (string.IsNullOrEmpty(_aeskey)) { _aeskey = AESKey; } byte[] keyArray = Encoding.UTF8.GetBytes(_aeskey); byte[] toEncryptArray = Encoding.UTF8.GetBytes(value); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateEncryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } /// <summary> /// AES解密 /// </summary> public static string AESDecrypt(string value, string _aeskey = null) { try { if (string.IsNullOrEmpty(_aeskey)) { _aeskey = AESKey; } byte[] keyArray = Encoding.UTF8.GetBytes(_aeskey); byte[] toEncryptArray = Convert.FromBase64String(value); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Encoding.UTF8.GetString(resultArray); } catch { return string.Empty; } } /// <summary> /// DES加密 /// </summary> public static string DESEncrypt(string value, string _deskey = null) { if (string.IsNullOrEmpty(_deskey)) { _deskey = DESKey; } byte[] keyArray = Encoding.UTF8.GetBytes(_deskey); byte[] toEncryptArray = Encoding.UTF8.GetBytes(value); DESCryptoServiceProvider rDel = new DESCryptoServiceProvider(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateEncryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } /// <summary> /// DES解密 /// </summary> public static string DESDecrypt(string value, string _deskey = null) { try { if (string.IsNullOrEmpty(_deskey)) { _deskey = DESKey; } byte[] keyArray = Encoding.UTF8.GetBytes(_deskey); byte[] toEncryptArray = Convert.FromBase64String(value); DESCryptoServiceProvider rDel = new DESCryptoServiceProvider(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Encoding.UTF8.GetString(resultArray); } catch { return string.Empty; } } public static string MD5(string value) { byte[] result = Encoding.UTF8.GetBytes(value); MD5 md5 = new MD5CryptoServiceProvider(); byte[] output = md5.ComputeHash(result); return BitConverter.ToString(output).Replace("-", ""); } public static string HMACMD5(string value, string hmacKey) { HMACSHA1 hmacsha1 = new HMACSHA1(Encoding.UTF8.GetBytes(hmacKey)); byte[] result = System.Text.Encoding.UTF8.GetBytes(value); byte[] output = hmacsha1.ComputeHash(result); return BitConverter.ToString(output).Replace("-", ""); } /// <summary> /// base64编码 /// </summary> /// <returns></returns> public static string Base64Encode(string value) { string result = Convert.ToBase64String(Encoding.Default.GetBytes(value)); return result; } /// <summary> /// base64解码 /// </summary> /// <returns></returns> public static string Base64Decode(string value) { string result = Encoding.Default.GetString(Convert.FromBase64String(value)); return result; } }



/* * 获取版本号工具类 */ public class ExeVersionUtils { /* * exe路径,返回当前版本号 */ public static string getOldVersionNumber(string exePath) { string FileVersions = ""; FileVersionInfo file1 = System.Diagnostics.FileVersionInfo.GetVersionInfo(exePath); //版本号显示为“主版本号.次版本号.内部版本号.专用部件号”。 FileVersions = String.Format("{0}.{1}.{2}.{3}", file1.FileMajorPart, file1.FileMinorPart, file1.FileBuildPart, file1.FilePrivatePart); return FileVersions; } /* * 检测是否需要升级本地的客户端,不需要返回"",需要直接返回下载地址 * 调用接口返回当前服务器最新的版本号 */ public static string CheckVersion(String LocalVersion, String loginName) { string downloadURL = ""; //把接口写了 string ServerVersion = HttpUitls.Get(""); if (CompareVersion(ServerVersion, LocalVersion)) { //下载最新客户端安装包的地址 downloadURL = ""; } return downloadURL; } /* * 版本号比较(使用C#自带的Version对象来比较版本号) * ServerVersion 服务器最新版本号,LocalVersion 本地程序版本号 */ public static bool CompareVersion(string ServerVersion,string LocalVersion) { bool flag = false; Version sv = new Version(ServerVersion); Version lv = new Version(LocalVersion); //服务器上面的客户端的版本号比本地客户端的版本号高。 if (sv > lv) { flag = true; } if (sv < lv) { flag = false; } return flag; } }
/* * 下载文件(显示下载百分比,winform控件) */ public class FileDownload { /// <summary> /// 下载文件 /// </summary> /// <param name="URL">下载文件地址</param> /// <param name="Filename">下载后的存放地址</param> /// <param name="Prog">用于显示的进度条</param> /// public static bool DownloadFile(string URL, string filename, System.Windows.Forms.ProgressBar prog, System.Windows.Forms.Label label1) { float percent = 0; try { System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL); System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse(); long totalBytes = myrp.ContentLength; if (prog != null) { prog.Maximum = (int)totalBytes; } System.IO.Stream st = myrp.GetResponseStream(); System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create); long totalDownloadedByte = 0; byte[] by = new byte[1024]; int osize = st.Read(by, 0, (int)by.Length); while (osize > 0) { totalDownloadedByte = osize + totalDownloadedByte; System.Windows.Forms.Application.DoEvents(); so.Write(by, 0, osize); if (prog != null) { prog.Value = (int)totalDownloadedByte; } osize = st.Read(by, 0, (int)by.Length); percent = (float)totalDownloadedByte / (float)totalBytes * 100; label1.Text = percent.ToString() + "%"; System.Windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息 } so.Close(); st.Close(); return true; } catch (System.Exception) { return false; throw; } } }
网友评论