using System; using System.IO; using System.Net; using System.Text; namespace ZigBee.Common { public static class CommonInfo { /// /// 当前登录用户 /// //public static ZigBee.Common.UserInfo LoginUser; public static Encoding EncodingUTF8 = Encoding.UTF8; public static Encoding EncodingGB2312 = Encoding.GetEncoding("gb2312"); public static string RequestHttpsHost = "https://developer.hdlcontrol.com"; /// /// 生成时间戳 /// public static string GetTimeStamp() { DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0)); DateTime nowTime = DateTime.Now; long unixTime = (long)System.Math.Round((nowTime - startTime).TotalMilliseconds, MidpointRounding.AwayFromZero); return unixTime.ToString(); } /// /// Unix时间戳转为C#格式时间 /// /// Unix时间戳格式,例如1482115779 /// C#格式时间 public static DateTime GetTime(string timeStamp) { DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); long lTime = long.Parse(timeStamp + "0000000"); TimeSpan toNow = new TimeSpan(lTime); return dtStart.Add(toNow); } /// /// DateTime时间格式转换为Unix时间戳格式 /// /// DateTime时间格式 /// Unix时间戳格式 public static long ConvertDateTimeLong(System.DateTime time) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); return (long)(time - startTime).TotalSeconds; } /// /// Get服务器方法 /// public static string GetMethod(string requestUrl) { string requestMethod = "get"; string urlHead = $"{RequestHttpsHost}/FeerView/"; string url = urlHead + requestUrl; HttpWebRequest httpWebRequestObj = WebRequest.CreateHttp(url); httpWebRequestObj.Method = requestMethod; using (HttpWebResponse httpWebResponseObj = httpWebRequestObj.GetResponse() as HttpWebResponse) { if (httpWebResponseObj == null) { return null; } else { Stream ResponseStream = httpWebResponseObj.GetResponseStream(); using (StreamReader Sr = new StreamReader(ResponseStream)) { string responseString = Sr.ReadToEnd(); return responseString; } } } } } }