123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215 |
- using System;
- using System.Data;
- using System.Web;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Security.Cryptography;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Net.Mail;
- using System.Net;
- using LitJson;
- using Microsoft.AspNetCore.Http;
- using ThoughtWorks.QRCode.Codec;
- using System.Linq;
- using System.IO;
- namespace Common
- {
- public class Function
- {
-
-
-
-
-
-
- public static string hmacSha1(string encryptText, string encryptKey)
- {
- HMACSHA1 myHMACSHA1 = new HMACSHA1(Encoding.UTF8.GetBytes(encryptKey));
- byte[] RstRes = myHMACSHA1.ComputeHash(Encoding.UTF8.GetBytes(encryptText));
- StringBuilder EnText = new StringBuilder();
- foreach (byte Byte in RstRes)
- {
- EnText.AppendFormat("{0:x2}", Byte);
- }
- return EnText.ToString();
- }
- public static string hmacmd5(string encryptText, string encryptKey)
- {
- HMACMD5 myHMACSHA1 = new HMACMD5(Encoding.UTF8.GetBytes(encryptKey));
- byte[] RstRes = myHMACSHA1.ComputeHash(Encoding.UTF8.GetBytes(encryptText));
- StringBuilder EnText = new StringBuilder();
- foreach (byte Byte in RstRes)
- {
- EnText.AppendFormat("{0:x2}", Byte);
- }
- return EnText.ToString();
- }
-
-
-
-
-
- public static string MD5_32(string str)
- {
- string cl = str + "@$1212#";
- string pwd = "";
- MD5 md5 = MD5.Create();
-
- byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
-
- for (int i = 0; i < s.Length; i++)
- {
-
- pwd = pwd + s[i].ToString("X").ToLower().PadLeft(2, '0');
- }
- return pwd;
- }
- public static string MD532(string str)
- {
- string cl = str;
- string pwd = "";
- MD5 md5 = MD5.Create();
-
- byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
-
- for (int i = 0; i < s.Length; i++)
- {
-
- pwd = pwd + s[i].ToString("X").ToLower().PadLeft(2, '0');
- }
- return pwd;
- }
-
-
-
-
-
- public static string MD5_16(string str)
- {
- return MD5_32(str).Substring(8, 16);
- }
-
-
-
-
- public static void WriteLog(string str)
- {
- try
- {
- string path = getPath("/log/message/" + DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Day.ToString() + "/");
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- StreamWriter sw = File.AppendText(path + "content.log");
- sw.WriteLine(str);
- sw.Flush();
- sw.Dispose();
- }
- catch
- { }
- }
-
-
-
-
- public static void WriteLog(string str, string filename)
- {
- try
- {
- string path = getPath("/log/" + filename + "/" + DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Day.ToString() + "/");
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- StreamWriter sw = File.AppendText(path + "content.log");
- sw.WriteLine(str);
- sw.Flush();
- sw.Dispose();
- }
- catch
- { }
- }
- public static void WriteLog(string path_str, string file_name, string page_content)
- {
- try
- {
- string path = getPath(path_str);
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- StreamWriter sw = File.AppendText(path + "/" + file_name);
- sw.WriteLine(page_content);
- sw.Flush();
- sw.Dispose();
- }
- catch
- { }
- }
-
-
-
-
- public static string NoHTML(string Htmlstring)
- {
- if (Htmlstring == null || Htmlstring == string.Empty)
- {
- return "";
- }
- Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
-
- Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
- Htmlstring = Regex.Replace(Htmlstring, @"&.*?;", "", RegexOptions.IgnoreCase);
- Htmlstring = Htmlstring.Replace("<", "〈");
- Htmlstring = Htmlstring.Replace(">", "〉");
-
-
- return Htmlstring;
- }
- public static bool IsIncludeChinese(string str)
- {
- Regex r = new Regex(@"[\u4E00-\u9FA5]", RegexOptions.IgnoreCase);
- if (r.IsMatch(str))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public static bool IsInt(string numberString)
- {
- if (string.IsNullOrEmpty(numberString))
- {
- return false;
- }
- Regex rCode = new Regex("^\\d+$");
- if (!rCode.IsMatch(numberString))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- public static string CheckInt(string numberString)
- {
- if (string.IsNullOrEmpty(numberString))
- {
- return "0";
- }
- Regex rCode = new Regex("^\\d+$");
- if (!rCode.IsMatch(numberString))
- {
- return "0";
- }
- else
- {
- return numberString;
- }
- }
- public static DateTime CheckDateTime(string numberString)
- {
- if (string.IsNullOrEmpty(numberString))
- {
- return DateTime.Now;
- }
- DateTime result;
- if (DateTime.TryParse(numberString, out result))
- {
- return result;
- }
- else
- {
- return DateTime.Now;
- }
- }
- public static bool ChkDateTime(string numberString)
- {
- if (string.IsNullOrEmpty(numberString))
- {
- return false;
- }
- DateTime result;
- if (DateTime.TryParse(numberString, out result))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public static string CheckNull(string numberString)
- {
- if (string.IsNullOrEmpty(numberString))
- {
- return "";
- }
- else
- {
- return numberString;
- }
- }
- public static string CheckUrl(string str)
- {
- if (str == null || str == string.Empty)
- {
- return "";
- }
- Regex rCode = new Regex(@"((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?");
- if (!rCode.IsMatch(str))
- {
- return "";
- }
- else
- {
- return str;
- }
- }
- public static string CheckEmail(string str)
- {
- if (str == null || str == string.Empty)
- {
- return "";
- }
- Regex rCode = new Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
- if (!rCode.IsMatch(str))
- {
- return "";
- }
- else
- {
- return str;
- }
- }
- public static string CheckMobile(string str)
- {
- if (str == null || str == string.Empty)
- {
- return "";
- }
- Regex rCode = new Regex(@"^1[3456789]\d{9}$");
- if (!rCode.IsMatch(str))
- {
- return "";
- }
- else
- {
- return str;
- }
- }
- public static string CheckIdCard(string str)
- {
- if (str == null || str == string.Empty)
- {
- return "";
- }
- Regex rCode = new Regex(@"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$");
- if (!rCode.IsMatch(str))
- {
- return "";
- }
- else
- {
- return str;
- }
- }
- public static bool IsNum(string numberString)
- {
- if (string.IsNullOrEmpty(numberString))
- {
- return false;
- }
- Regex rCode = new Regex(@"^\d+(\.\d+)?$");
- if (!rCode.IsMatch(numberString))
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- public static string CheckNum(string numberString)
- {
- if (string.IsNullOrEmpty(numberString))
- {
- return "0";
- }
- Regex rCode = new Regex(@"^\d+(\.\d+)?$");
- if (!rCode.IsMatch(numberString))
- {
- return "0";
- }
- else
- {
- return numberString;
- }
- }
- public static string CheckString(string str)
- {
- if (string.IsNullOrEmpty(str))
- {
- return "";
- }
- str = str.Replace("'", "´");
- str = str.Replace("\"", """);
-
- str = str.Replace("<", "<");
- str = str.Replace(">", ">");
- str = str.Replace("(", "(");
- str = str.Replace(")", ")");
- str = ToDBC(str);
- return str;
- }
- public static string unCheckString(string str)
- {
- if (str == null || str == string.Empty)
- {
- return "";
- }
- str = str.Replace("´", "'");
- str = str.Replace(""", "\"");
-
- str = str.Replace("<", "<");
- str = str.Replace(">", ">");
- str = str.Replace("(", "(");
- str = str.Replace(")", ")");
- return str;
- }
- public static string CheckString2(string str)
- {
- if (str == null || str == string.Empty)
- {
- return "";
- }
- str = ToDBC(str);
- str = Regex.Replace(str, @"<script.*?>[\s\S]*?</script>", "", RegexOptions.IgnoreCase);
- str = Regex.Replace(str, @"<script.*?>", "", RegexOptions.IgnoreCase);
- str = Regex.Replace(str, @"<object.*?>[\s\S]*?</object>", "", RegexOptions.IgnoreCase);
- str = Regex.Replace(str, @"<object.*?>", "", RegexOptions.IgnoreCase);
- str = Regex.Replace(str, @"<iframe.*?>[\s\S]*?</iframe>", "", RegexOptions.IgnoreCase);
- str = Regex.Replace(str, @"<frameset.*?>[\s\S]*?</frameset>", "", RegexOptions.IgnoreCase);
- str = Regex.Replace(str, @"<frameset.*?>", "", RegexOptions.IgnoreCase);
- str = Regex.Replace(str, @"<frame.*?>[\s\S]*?</frame>", "", RegexOptions.IgnoreCase);
- str = Regex.Replace(str, @"<frame.*?>", "", RegexOptions.IgnoreCase);
- str = Regex.Replace(str, @"<form.*?>[\s\S]*?</form>", "", RegexOptions.IgnoreCase);
- str = Regex.Replace(str, @"<input.*?>", "", RegexOptions.IgnoreCase);
- str = Regex.Replace(str, @"<select.*?>[\s\S]*?</select>", "", RegexOptions.IgnoreCase);
- str = Regex.Replace(str, @"<textarea.*?>[\s\S]*?</textarea>", "", RegexOptions.IgnoreCase);
- str = Regex.Replace(str, @"<button.*?>", "", RegexOptions.IgnoreCase);
- str = Regex.Replace(str, @"<noframes.*?>[\s\S]*?</noframes>", "", RegexOptions.IgnoreCase);
- str = Regex.Replace(str, @"<noframes.*?>", "", RegexOptions.IgnoreCase);
- return str;
- }
- public static string GetSession(HttpContext context, string strName)
- {
- var value = context.Session.GetString(strName);
- if (string.IsNullOrEmpty(value))
- {
- value = "";
- }
- return value;
- }
- public static void WriteSession(HttpContext context, string strName, string strValue)
- {
- context.Session.SetString(strName, strValue);
- }
- public static void DelSession(HttpContext context, string strName)
- {
- context.Session.Remove(strName);
- }
- public static string rootIndex = System.AppDomain.CurrentDomain.BaseDirectory;
- private static string Read(string absoluteFileLocation)
- {
- string result = "";
- if (System.IO.File.Exists(absoluteFileLocation))
- {
- using (FileStream fs = new FileStream(absoluteFileLocation, FileMode.Open, FileAccess.Read))
- {
- using (StreamReader sr = new StreamReader(fs, System.Text.Encoding.UTF8))
- {
- try
- {
- result = sr.ReadToEnd();
-
- }
- catch
- { }
- }
- }
- }
- return result;
- }
- public static string getPath(string path_str)
- {
- string result = AppContext.BaseDirectory + path_str;
- return result;
- }
- public static string ReadInstance(string path_str)
- {
-
- string path = getPath(path_str);
- string content = "";
- content = Read(path);
- return content;
- }
- public static string ReadInstanceNoAuth(string path_str)
- {
-
- string path = getPath(path_str);
- string content = "";
- content = Read(path);
- return content;
- }
- public static string ReadInstanceByFull(string path_str)
- {
- string content = "";
- content = Read(path_str);
- return content;
- }
- public static void WritePage(string path_str, string file_name, string page_content)
- {
- try
- {
- string path = getPath(path_str);
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- Encoding nobom = new UTF8Encoding(false, false);
- StreamWriter sw = new StreamWriter(path + "/" + file_name, false, nobom);
- sw.Write(page_content);
- sw.Flush();
- sw.Dispose();
- }
- catch
- { }
- }
- public static void WritePageFullPath(string path_str, string file_name, string page_content)
- {
- try
- {
- if (!Directory.Exists(path_str))
- {
- Directory.CreateDirectory(path_str);
- }
- Encoding nobom = new UTF8Encoding(false, false);
- StreamWriter sw = new StreamWriter(path_str + file_name, false, nobom);
- sw.Write(page_content);
- sw.Flush();
- sw.Dispose();
- }
- catch
- { }
- }
- public static void send_email(string mailTitle, string mailTo, string mailContent, string file_path, string host, int port, string username, string pwd, string displayname)
- {
- try
- {
- MailAddress from = new MailAddress(username, displayname);
- MailMessage mail = new MailMessage();
-
- mail.Subject = mailTitle;
- mail.SubjectEncoding = Encoding.UTF8;
-
-
- mail.From = from;
-
- string address = "";
- string displayName = "";
-
-
- string[] mailNames = (mailTo + ";").Split(';');
- foreach (string name in mailNames)
- {
- if (name != string.Empty)
- {
- if (name.IndexOf('<') > 0)
- {
- displayName = name.Substring(0, name.IndexOf('<'));
- address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
- }
- else
- {
- displayName = string.Empty;
- address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
- }
- mail.To.Add(new MailAddress(address, displayName));
- }
- }
-
-
-
-
- mail.Body = mailContent;
-
- mail.BodyEncoding = Encoding.UTF8;
- mail.IsBodyHtml = true;
-
- mail.Priority = MailPriority.Normal;
-
-
-
-
-
- mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
- SmtpClient client = new SmtpClient();
-
-
- client.Host = host;
-
-
- client.Port = port;
- client.UseDefaultCredentials = false;
- client.EnableSsl = true;
-
- client.Credentials = new System.Net.NetworkCredential(username, pwd);
- client.DeliveryMethod = SmtpDeliveryMethod.Network;
- client.Send(mail);
- }
- catch (Exception ex)
- {
- Function.WriteLog(ex.ToString());
- }
- }
- public static string GetWebRequest(string url)
- {
- return GetWebRequest(url, new Dictionary<string, string>());
- }
- public static string GetWebRequest(string url, Dictionary<string, string> header)
- {
- string result = "";
- try
- {
- HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(url);
- webReq.Method = "GET";
- webReq.KeepAlive = true;
- webReq.Timeout = 1200000;
- webReq.ContentType = "text/html";
- if (header.Count > 0)
- {
- foreach (string key in header.Keys)
- {
- webReq.Headers.Add(key, header[key]);
- }
- }
- webReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
- using (HttpWebResponse response = (HttpWebResponse)webReq.GetResponse())
- {
- using (StreamReader reader = new StreamReader(response.GetResponseStream()))
- {
- result = reader.ReadToEnd();
-
- }
- if (response != null)
- response.Close();
- }
- }
- catch (Exception ex)
- {
- result = ex.ToString();
- }
- return result;
- }
- public static string get_Random(int num)
- {
- string[] str = new string[num];
- string serverCode = "";
-
- Random random = new Random(GetRandomSeed());
- for (int i = 0; i < num; i++)
- {
- str[i] = random.Next(10).ToString().Substring(0, 1);
- }
- foreach (string s in str)
- {
- serverCode += s;
- }
- return serverCode;
- }
- public static int get_Random(int min, int max)
- {
- int serverCode = 0;
- Random random = new Random(GetRandomSeed());
- serverCode = random.Next(max - min) + min;
- return serverCode;
- }
- public static string get_Random_string(int count)
- {
- string str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
- string result = "";
- for (int i = 0; i < count; i++)
- {
- Random r = new Random(GetRandomSeed());
- int serverCode = r.Next(str.Length - 0) + 0;
- result += str.Substring(serverCode, 1);
- }
- return result;
- }
- public static int GetRandomSeed()
- {
-
- byte[] bytes = new byte[4];
-
- System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
-
- rng.GetBytes(bytes);
-
- return BitConverter.ToInt32(bytes, 0);
- }
- public static string get_weekday(DateTime date)
- {
- string[] week = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
- return week[(int)date.DayOfWeek];
- }
- public static string get_substr(string s, int length)
- {
- byte[] bytes = System.Text.Encoding.Unicode.GetBytes(s);
- int n = 0;
- int i = 0;
- for (; i < bytes.GetLength(0) && n < length; i++)
- {
-
- if (i % 2 == 0)
- {
- n++;
- }
- else
- {
-
- if (bytes[i] > 0)
- {
- n++;
- }
- }
- }
-
- if (i % 2 == 1)
- {
-
- if (bytes[i] > 0)
- i = i - 1;
-
- else
- i = i + 1;
- }
- return System.Text.Encoding.Unicode.GetString(bytes, 0, i);
- }
- public static DateTime ConvertIntDateTime(double d)
- {
- DateTime time = DateTime.MinValue;
- DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
- time = startTime.AddSeconds(d);
- return time;
- }
- public static DateTime ConvertIntDateTimeMini(long TimeStamp)
- {
- System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
- return startTime.AddTicks(TimeStamp * 10000);
- }
- public static int ConvertDateTimeInt(DateTime time)
- {
- double intResult = 0;
- DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
- TimeSpan ts = time - startTime;
- intResult = Math.Round(ts.TotalSeconds, 0);
- return int.Parse(intResult.ToString());
- }
- public static long GetCurTimestamp()
- {
- var ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
- long times = Convert.ToInt64(ts.TotalMilliseconds);
- return times;
- }
- public static DateTime checkDateTimeNull(DateTime? datetime)
- {
- if (datetime != null)
- {
- return datetime.Value;
- }
- return DateTime.Now;
- }
- public static bool IsDate(string datetime)
- {
- DateTime check = DateTime.Now;
- return DateTime.TryParse(datetime + " 00:00:00", out check);
- }
- public static bool IsDateTime(string datetime)
- {
- DateTime check = DateTime.Now;
- return DateTime.TryParse(datetime, out check);
- }
-
-
-
-
- public static string getTimeStamp()
- {
- TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
- return Convert.ToInt64(ts.TotalSeconds).ToString();
- }
- public static string get_timespan(DateTime s, string show_type)
- {
- string result = get_timespan(s, DateTime.Now, show_type);
- return result;
- }
- public static string get_timespan(DateTime s, DateTime e, string show_type)
- {
- string result = "";
- if (e > s)
- {
- int total = 0;
- TimeSpan ts = e - s;
- switch (show_type)
- {
- case "d":
- result = ts.Days.ToString() + "天";
- break;
- case "h":
- if (ts.Days > 0)
- {
- total += ts.Days * 24;
- }
- total += ts.Hours;
- result = total.ToString() + "小时";
- break;
- case "m":
- if (ts.Days > 0)
- {
- total += ts.Days * 24 * 60;
- }
- if (ts.Hours > 0)
- {
- total += ts.Hours * 60;
- }
- total += ts.Hours;
- result = total.ToString() + "分钟";
- break;
- case "s":
- if (ts.Days > 0)
- {
- total += ts.Days * 24 * 60 * 60;
- }
- if (ts.Hours > 0)
- {
- total += ts.Hours * 60 * 60;
- }
- if (ts.Minutes > 0)
- {
- total += ts.Hours * 60;
- }
- total += ts.Hours;
- result = total.ToString() + "秒";
- break;
- case "time":
- if (ts.Days > 1)
- {
- result = s.ToString("yyyy-MM-dd");
- }
- else
- {
- if (ts.Days > 0)
- {
- result += ts.Days + "天";
- }
- if (ts.Hours > 0)
- {
- result += ts.Hours + "小时";
- }
- if (ts.Minutes > 0)
- {
- result += ts.Minutes + "分钟";
- }
- if (string.IsNullOrEmpty(result))
- {
- result = "刚刚";
- }
- else
- {
- result += "前";
- }
- }
- break;
- default: break;
- }
- }
- return result;
- }
-
- public static string ToSBC(string input)
- {
- char[] c = input.ToCharArray();
- for (int i = 0; i < c.Length; i++)
- {
- if (c[i] == 32)
- {
- c[i] = (char)12288;
- continue;
- }
- if (c[i] < 127)
- c[i] = (char)(c[i] + 65248);
- }
- return new string(c);
- }
-
- public static string ToDBC(string input)
- {
- char[] c = input.ToCharArray();
- for (int i = 0; i < c.Length; i++)
- {
- if (c[i] == 12288)
- {
- c[i] = (char)32;
- continue;
- }
- if (c[i] > 65280 && c[i] < 65375)
- c[i] = (char)(c[i] - 65248);
- }
- return new string(c);
- }
- public static string PostWebRequest(string postUrl, string paramData, string ContentType = "application/x-www-form-urlencoded")
- {
-
- string ret = string.Empty;
- try
- {
- byte[] postData = Encoding.UTF8.GetBytes(paramData);
-
- HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
- Encoding myEncoding = Encoding.UTF8;
- request.Method = "POST";
- request.KeepAlive = false;
- request.AllowAutoRedirect = true;
- request.ContentType = ContentType;
-
- request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
- request.ContentLength = postData.Length;
-
- System.IO.Stream outputStream = request.GetRequestStream();
- outputStream.Write(postData, 0, postData.Length);
- outputStream.Close();
- HttpWebResponse response;
- Stream responseStream;
- StreamReader reader;
- string srcString;
- response = request.GetResponse() as HttpWebResponse;
- responseStream = response.GetResponseStream();
- reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
- srcString = reader.ReadToEnd();
- ret = srcString;
- reader.Close();
- }
- catch (Exception ex)
- {
- ret = "fail";
- WriteLog(ex.ToString(), "PostWebRequest");
- }
- return ret;
- }
- public static string PostWebRequest(string postUrl, string paramData, Dictionary<string, string> header, string ContentType = "application/x-www-form-urlencoded")
- {
-
- string ret = string.Empty;
- try
- {
- byte[] postData = Encoding.UTF8.GetBytes(paramData);
-
- HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
- Encoding myEncoding = Encoding.UTF8;
- request.Method = "POST";
- request.KeepAlive = false;
- request.AllowAutoRedirect = true;
- request.ContentType = ContentType;
-
- request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
- request.ContentLength = postData.Length;
- if (header.Count > 0)
- {
- foreach (string key in header.Keys)
- {
- request.Headers.Add(key, header[key]);
- }
- }
-
- System.IO.Stream outputStream = request.GetRequestStream();
- outputStream.Write(postData, 0, postData.Length);
- outputStream.Close();
- HttpWebResponse response;
- Stream responseStream;
- StreamReader reader;
- string srcString;
- response = request.GetResponse() as HttpWebResponse;
- responseStream = response.GetResponseStream();
- reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
- srcString = reader.ReadToEnd();
- ret = srcString;
- reader.Close();
- }
- catch (Exception ex)
- {
- ret = "fail";
- WriteLog(ex.ToString(), "PostWebRequest");
- }
- return ret;
- }
-
-
-
-
-
- public static List<string> GetFtpDirctoryList(string path, string username, string password)
- {
- List<string> strs = new List<string>();
- try
- {
- string uri = path;
- FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
-
- reqFTP.Credentials = new NetworkCredential(username, password);
- reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
- WebResponse response = reqFTP.GetResponse();
- StreamReader reader = new StreamReader(response.GetResponseStream());
-
- string line = reader.ReadLine();
- while (line != null)
- {
- if (line.Contains("<DIR>"))
- {
- string msg = line.Substring(line.LastIndexOf("<DIR>") + 5).Trim();
- strs.Add(msg);
- }
- line = reader.ReadLine();
- }
- reader.Close();
- response.Close();
- return strs;
- }
- catch (Exception ex)
- {
- Function.WriteLog(DateTime.Now + ":" + ex.ToString(), "从ftp服务器上获得文件夹列表异常");
- }
- return strs;
- }
-
-
-
-
-
-
- public static List<string> GetFtpFileList(string path, string username, string password)
- {
- List<string> strs = new List<string>();
- try
- {
- string uri = path;
- FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
-
- reqFTP.Credentials = new NetworkCredential(username, password);
- reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
- WebResponse response = reqFTP.GetResponse();
- StreamReader reader = new StreamReader(response.GetResponseStream());
-
- string line = reader.ReadLine();
- while (line != null)
- {
- if (!line.Contains("<DIR>"))
- {
- string msg = line.Substring(39).Trim();
- strs.Add(msg);
- }
- line = reader.ReadLine();
- }
- reader.Close();
- response.Close();
- return strs;
- }
- catch (Exception ex)
- {
- Function.WriteLog(DateTime.Now + ":" + ex.ToString(), "从ftp服务器上获得文件列表异常");
- }
- return strs;
- }
-
- public static string FtpDownload(string path, string fileName, string username, string password)
- {
- FtpWebRequest reqFTP;
- string savePath = "";
- try
- {
- string filePath = getPath("/FtpDownloadFile/" + fileName);
- FileStream outputStream = new FileStream(filePath, FileMode.Create);
- reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path + fileName));
- reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
- reqFTP.UseBinary = true;
- reqFTP.Credentials = new NetworkCredential(username, password);
- reqFTP.UsePassive = false;
- FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
- Stream ftpStream = response.GetResponseStream();
- long cl = response.ContentLength;
- int bufferSize = 2048;
- int readCount;
- byte[] buffer = new byte[bufferSize];
- readCount = ftpStream.Read(buffer, 0, bufferSize);
- while (readCount > 0)
- {
- outputStream.Write(buffer, 0, readCount);
- readCount = ftpStream.Read(buffer, 0, bufferSize);
- }
- ftpStream.Close();
- outputStream.Close();
- response.Close();
- savePath = "/FtpDownloadFile/" + fileName;
- }
- catch (Exception ex)
- {
- Function.WriteLog(DateTime.Now + ":" + ex.ToString(), "从ftp服务器上下载文件异常");
- }
- return savePath;
- }
- public static string base64StringToImage(string base64String, string path, string filename)
- {
- string fullpath = getPath(path);
- if (!System.IO.Directory.Exists(fullpath))
- {
- System.IO.Directory.CreateDirectory(fullpath);
- }
- byte[] arr = Convert.FromBase64String(base64String);
- System.IO.File.WriteAllBytes(fullpath + filename, arr);
- return path + filename;
- }
- public static String BuildQueryString(SortedList<String, String> kvp)
- {
- return String.Join("&", kvp.Select(item => String.Format("{0}={1}", item.Key.Trim(), item.Value)).ToArray());
- }
- #region 获取网络文件内容
- public static string GetNetFileContent(string url)
- {
- string textContent = "";
- using (var client = new WebClient())
- {
- try
- {
- textContent = client.DownloadString(url);
- }
- catch (Exception ex)
- {
- WriteLog(DateTime.Now.ToString() + "\n" + url + "\n" + ex.ToString() + "\n\n", "获取网络文件内容异常");
- }
- }
- return textContent;
- }
- public static byte[] GetNetFileData(string url)
- {
- byte[] textContent = new byte[] { };
- using (var client = new WebClient())
- {
- try
- {
- textContent = client.DownloadData(url);
- }
- catch (Exception ex)
- {
- WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "获取网络文件流异常");
- }
- }
- return textContent;
- }
- #endregion
- }
- }
|