/******************************************************************************* * Copyright © 2016 HT.Cloud.Framework 版权所有 * Author: HT.Cloud * Description: WaterCloud快速开发平台 * Website: *********************************************************************************/ using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; using System.Net; using System.Net.Http; using System.Net.Sockets; using System.Text; using System.Text.Encodings.Web; using System.Text.RegularExpressions; using System.Web; namespace HT.Cloud.Code { public class WebHelper { #region ResolveUrl(解析相对Url) /// /// 解析相对Url /// /// 相对Url public static string ResolveUrl(string relativeUrl) { if (string.IsNullOrWhiteSpace(relativeUrl)) return string.Empty; relativeUrl = relativeUrl.Replace("\\", "/"); if (relativeUrl.StartsWith("/")) return relativeUrl; if (relativeUrl.Contains("://")) return relativeUrl; return VirtualPathUtility.ToAbsolute(relativeUrl); } #endregion ResolveUrl(解析相对Url) #region HtmlEncode(对html字符串进行编码) /// /// 对html字符串进行编码 /// /// html字符串 public static string HtmlEncode(string html) { return HttpUtility.HtmlEncode(html); } /// /// 对html字符串进行解码 /// /// html字符串 public static string HtmlDecode(string html) { return HttpUtility.HtmlDecode(html); } #endregion HtmlEncode(对html字符串进行编码) #region UrlEncode(对Url进行编码) /// /// 对Url进行编码 /// /// url /// 编码字符是否转成大写,范例,"http://"转成"http%3A%2F%2F" public static string UrlEncode(string url, bool isUpper = false) { return UrlEncode(url, Encoding.UTF8, isUpper); } /// /// 对Url进行编码 /// /// url /// 字符编码 /// 编码字符是否转成大写,范例,"http://"转成"http%3A%2F%2F" public static string UrlEncode(string url, Encoding encoding, bool isUpper = false) { var result = HttpUtility.UrlEncode(url, encoding); if (!isUpper) return result; return GetUpperEncode(result); } /// /// 获取大写编码字符串 /// private static string GetUpperEncode(string encode) { var result = new StringBuilder(); int index = int.MinValue; for (int i = 0; i < encode.Length; i++) { string character = encode[i].ToString(); if (character == "%") index = i; if (i - index == 1 || i - index == 2) character = character.ToUpper(); result.Append(character); } return result.ToString(); } #endregion UrlEncode(对Url进行编码) #region UrlDecode(对Url进行解码) /// /// 对Url进行解码,对于javascript的encodeURIComponent函数编码参数,应使用utf-8字符编码来解码 /// /// url public static string UrlDecode(string url) { return HttpUtility.UrlDecode(url); } /// /// 对Url进行解码,对于javascript的encodeURIComponent函数编码参数,应使用utf-8字符编码来解码 /// /// url /// 字符编码,对于javascript的encodeURIComponent函数编码参数,应使用utf-8字符编码来解码 public static string UrlDecode(string url, Encoding encoding) { return HttpUtility.UrlDecode(url, encoding); } #endregion UrlDecode(对Url进行解码) #region Session操作 /// /// 写Session /// /// Session的键名 /// Session的键值 public static void WriteSession(string key, string value) { if (key.IsEmpty()) return; GlobalContext.HttpContext?.Session.SetString(key, value); } /// /// 读取Session的值 /// /// Session的键名 public static string GetSession(string key) { if (string.IsNullOrEmpty(key)) { return string.Empty; } return GlobalContext.HttpContext?.Session.GetString(key) ?? ""; } /// /// 删除指定Session /// /// Session的键名 public static void RemoveSession(string key) { if (string.IsNullOrEmpty(key)) { return; } GlobalContext.HttpContext?.Session.Remove(key); } #endregion Session操作 #region Cookie操作 /// /// 写cookie值 /// /// 名称 /// 值 public static void WriteCookie(string strName, string strValue, CookieOptions option = null) { if (option == null) { option = new CookieOptions(); option.Expires = DateTime.Now.AddDays(30); } GlobalContext.HttpContext?.Response.Cookies.Append(strName, strValue, option); } /// /// 写cookie值 /// /// 名称 /// 值 /// 过期时间(分钟) public static void WriteCookie(string strName, string strValue, int expires) { CookieOptions option = new CookieOptions(); option.Expires = DateTime.Now.AddMinutes(expires); GlobalContext.HttpContext?.Response.Cookies.Append(strName, strValue, option); } /// /// 读cookie值 /// /// 名称 /// cookie值 public static string GetCookie(string strName) { return GlobalContext.HttpContext?.Request.Cookies[strName] ?? ""; } /// /// 删除Cookie对象 /// /// Cookie对象名称 public static void RemoveCookie(string CookiesName) { GlobalContext.HttpContext?.Response.Cookies.Delete(CookiesName); } #endregion Cookie操作 #region 去除HTML标记 /// /// 去除HTML标记 /// /// 包括HTML的源码 /// 已经去除后的文字 public static string NoHtml(string Htmlstring) { //删除脚本 Htmlstring = Regex.Replace(Htmlstring, @"]*?>.*?", "", RegexOptions.IgnoreCase); //删除HTML Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase); Htmlstring = Regex.Replace(Htmlstring, @"