zoukankan      html  css  js  c++  java
  • C#一些实用的函数

    1.DateTime 转为Unix的long的时间戳

    long orderTime = order.AddTime.ToUnixTimeStamp("Milliseconds");long payTime = order.StartTime.Value.ToUnixTimeStamp("Milliseconds");

    2、获取客户端IP

            /// <summary>

           /// 获取调用方的IP地址

           /// </summary>

           /// <param name="request"></param>

           /// <returns></returns>

            public static string GetClientIp(HttpRequestMessage request)

            {

                if (request.Properties.ContainsKey("MS_HttpContext"))

                {

                    return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;

                }


                if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))

                {

                    RemoteEndpointMessageProperty prop;

                    prop = (RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name];

                    return prop.Address;

                }

                return null;

            }

    3、获取当前站点域名

     

    /// <summary>

           /// 获取当前站点域名

           /// </summary>

           /// <returns></returns>

            public static string GetCurrentDomain()

            {

                var url = HttpContext.Current.Request.Url;

                return string.Format("{0}://{1}:{2}", url.Scheme, url.Host, url.Port);

            }

    4、Md5加密

    /// <summary>

            /// MD5加密

            /// </summary>

            /// <param name="strInput">加密前的字符串</param>

            /// <returns></returns>

            public static string MD5(string strInput)

            {

                using (MD5 md5 = new MD5CryptoServiceProvider())

                {

                    byte[] bytResult = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strInput));

                    string strResult = BitConverter.ToString(bytResult);

                    strResult = strResult.Replace("-", "");

                    return strResult.ToLower();

                }

            }

    5、UrlEncode和UrlDecode

            /// <summary>

            /// UrlEncode

            /// </summary>

            /// <param name="strInput">Encode前的字符串</param>

            /// <returns></returns>

            public static string UrlEncode(string strInput)

            {

               return System.Web.HttpUtility.UrlEncode(strInput, Encoding.UTF8);

            }

            /// <summary>

            /// UrlDecode

            /// </summary>

            /// <param name="strInput">Decode前的字符串</param>

            /// <returns></returns>

            public static string UrlDecode(string strInput)

            {

                return System.Web.HttpUtility.UrlDecode(strInput, Encoding.UTF8);

            }


  • 相关阅读:
    iOS 获取当前设备类
    iOS 字体详解
    iOS屏幕旋转总结
    CocoaPods安装和使用教程
    【iOS】build diff: /../Podfile.lock: No such file or directory
    Eclipse使用Maven构建web项目
    UIScrollView增加刷新
    Mac下打开eclipse 始终提示 你需要安装Java SE 6 Runtime
    嵌入支付宝SDK,出现“LaunchServices: ERROR: There is no registered handler for URL scheme alipay”错误
    iOS开发 传感器(加速计、摇一摇、计步器)
  • 原文地址:https://www.cnblogs.com/hgmyz/p/12351564.html
Copyright © 2011-2022 走看看