1.DateTime 转为Unix的long的时间戳
long orderTime = order.AddTime.ToUnixTimeStamp("Milliseconds");long payTime = order.StartTime.Value.ToUnixTimeStamp("Milliseconds");
2、获取客户端IP
///
/// 获取调用方的IP地址
///
///
///
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、获取当前站点域名
///
/// 获取当前站点域名
///
///
public static string GetCurrentDomain()
{
var url = HttpContext.Current.Request.Url;
return string.Format("{0}://{1}:{2}", url.Scheme, url.Host, url.Port);
}
4、Md5加密
///
/// MD5加密
///
/// 加密前的字符串
///
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
///
/// UrlEncode
///
/// Encode前的字符串
///
public static string UrlEncode(string strInput)
{
return System.Web.HttpUtility.UrlEncode(strInput, Encoding.UTF8);
}
///
/// UrlDecode
///
/// Decode前的字符串
///
public static string UrlDecode(string strInput)
{
return System.Web.HttpUtility.UrlDecode(strInput, Encoding.UTF8);
}