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);
}
标签:return,函数,strInput,C#,request,实用,static,public,string 来源: https://blog.51cto.com/u_11205641/2827190