using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
namespace NFine.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
#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
#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
#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
#region Session操作
///
/// 写Session
///
/// Session键值的类型
/// Session的键名
/// Session的键值
public static void WriteSession(string key, T value)
{
if (key.IsEmpty())
return;
HttpContext.Current.Session[key] = value;
}
///
/// 写Session
///
/// Session的键名
/// Session的键值
public static void WriteSession(string key, string value)
{
WriteSession(key, value);
}
///
/// 读取Session的值
///
/// Session的键名
public static string GetSession(string key)
{
if (key.IsEmpty())
return string.Empty;
return HttpContext.Current.Session[key] as string;
}
///
/// 删除指定Session
///
/// Session的键名
public static void RemoveSession(string key)
{
if (key.IsEmpty())
return;
HttpContext.Current.Session.Contents.Remove(key);
}
#endregion
#region Cookie操作
///
/// 写cookie值
///
/// 名称
/// 值
public static void WriteCookie(string strName, string strValue)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie.Value = strValue;
HttpContext.Current.Response.AppendCookie(cookie);
}
///
/// 写cookie值
///
/// 名称
/// 值
/// 过期时间(分钟)
public static void WriteCookie(string strName, string strValue, int expires)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
if (cookie == null)
{
cookie = new HttpCookie(strName);
}
cookie.Value = strValue;
cookie.Expires = DateTime.Now.AddMinutes(expires);
HttpContext.Current.Response.AppendCookie(cookie);
}
///
/// 读cookie值
///
/// 名称
/// cookie值
public static string GetCookie(string strName)
{
if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
{
return HttpContext.Current.Request.Cookies[strName].Value.ToString();
}
return "";
}
///
/// 删除Cookie对象
///
/// Cookie对象名称
public static void RemoveCookie(string CookiesName)
{
HttpCookie objCookie = new HttpCookie(CookiesName.Trim());
objCookie.Expires = DateTime.Now.AddYears(-5);
HttpContext.Current.Response.Cookies.Add(objCookie);
}
#endregion
#region GetFileControls(获取客户端文件控件集合)
///
/// 获取有效客户端文件控件集合,文件控件必须上传了内容,为空将被忽略,
/// 注意:Form标记必须加入属性 enctype="multipart/form-data",服务器端才能获取客户端file控件.
///
public static List GetFileControls()
{
var result = new List();
var files = HttpContext.Current.Request.Files;
if (files.Count == 0)
return result;
for (int i = 0; i < files.Count; i++)
{
var file = files[i];
if (file.ContentLength == 0)
continue;
result.Add(files[i]);
}
return result;
}
#endregion
#region GetFileControl(获取第一个有效客户端文件控件)
///
/// 获取第一个有效客户端文件控件,文件控件必须上传了内容,为空将被忽略,
/// 注意:Form标记必须加入属性 enctype="multipart/form-data",服务器端才能获取客户端file控件.
///
public static HttpPostedFile GetFileControl()
{
var files = GetFileControls();
if (files == null || files.Count == 0)
return null;
return files[0];
}
#endregion
#region HttpWebRequest(请求网络资源)
///
/// 请求网络资源,返回响应的文本
///
/// 网络资源地址
public static string HttpWebRequest(string url)
{
return HttpWebRequest(url, string.Empty, Encoding.GetEncoding("utf-8"));
}
///
/// 请求网络资源,返回响应的文本
///
/// 网络资源Url地址
/// 提交的参数,格式:参数1=参数值1&参数2=参数值2
public static string HttpWebRequest(string url, string parameters)
{
return HttpWebRequest(url, parameters, Encoding.GetEncoding("utf-8"), true);
}
///
/// 请求网络资源,返回响应的文本
///
/// 网络资源地址
/// 提交的参数,格式:参数1=参数值1&参数2=参数值2
/// 字符编码
/// 是否Post提交
/// 内容类型
/// Cookie容器
/// 超时时间
public static string HttpWebRequest(string url, string parameters, Encoding encoding, bool isPost = false,
string contentType = "application/x-www-form-urlencoded", CookieContainer cookie = null, int timeout = 120000)
{
var request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = timeout;
request.CookieContainer = cookie;
if (isPost)
{
byte[] postData = encoding.GetBytes(parameters);
request.Method = "POST";
request.ContentType = contentType;
request.ContentLength = postData.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(postData, 0, postData.Length);
}
}
var response = (HttpWebResponse)request.GetResponse();
string result;
using (Stream stream = response.GetResponseStream())
{
if (stream == null)
return string.Empty;
using (var reader = new StreamReader(stream, encoding))
{
result = reader.ReadToEnd();
}
}
return result;
}
#endregion
#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, @"