C#自定义针对URL地址的处理类实例
程序员文章站
2023-12-12 15:14:34
本文实例讲述了c#自定义针对url地址的处理类。分享给大家供大家参考。具体分析如下:
这个c#类是专门针对url网址处理的类,可以对url地址进行base64的加密和解密...
本文实例讲述了c#自定义针对url地址的处理类。分享给大家供大家参考。具体分析如下:
这个c#类是专门针对url网址处理的类,可以对url地址进行base64的加密和解密,可以通过函数的方式向url添加参数,可以更新url中已有参数的值,分析url地址的域名、子域名,分析url的所有参数和参数值,功能非常全面,还可以自己扩充,非常具有实用价值
using system; using system.text.regularexpressions; using system.web; using system.collections.specialized; namespace dotnet.utilities { /// <summary> /// url的操作类 /// </summary> public class urloper { static system.text.encoding encoding = system.text.encoding.utf8; #region url的64位编码 public static string base64encrypt(string sourthurl) { string eurl = httputility.urlencode(sourthurl); eurl = convert.tobase64string(encoding.getbytes(eurl)); return eurl; } #endregion #region url的64位解码 public static string base64decrypt(string estr) { if (!isbase64(estr)) { return estr; } byte[] buffer = convert.frombase64string(estr); string sourthurl = encoding.getstring(buffer); sourthurl = httputility.urldecode(sourthurl); return sourthurl; } /// <summary> /// 是否是base64字符串 /// </summary> /// <param name="estr"></param> /// <returns></returns> public static bool isbase64(string estr) { if ((estr.length % 4) != 0) { return false; } if (!regex.ismatch(estr, "^[a-z0-9/+=]*$", regexoptions.ignorecase)) { return false; } return true; } #endregion /// <summary> /// 添加url参数 /// </summary> public static string addparam(string url, string paramname, string value) { uri uri = new uri(url); if (string.isnullorempty(uri.query)) { string eval = httpcontext.current.server.urlencode(value); return string.concat(url, "?" + paramname + "=" + eval); } else { string eval = httpcontext.current.server.urlencode(value); return string.concat(url, "&" + paramname + "=" + eval); } } /// <summary> /// 更新url参数 /// </summary> public static string updateparam(string url, string paramname, string value) { string keyword = paramname+"="; int index = url.indexof(keyword)+keyword.length; int index1 = url.indexof("&", index); if (index1 == -1) { url = url.remove(index, url.length - index); url = string.concat(url, value); return url; } url = url.remove(index,index1 - index); url = url.insert(index, value); return url; } #region 分析url所属的域 public static void getdomain(string fromurl, out string domain, out string subdomain) { domain = ""; subdomain = ""; try { if (fromurl.indexof("的名片") > -1) { subdomain = fromurl; domain = "名片"; return; } uribuilder builder = new uribuilder(fromurl); fromurl = builder.tostring(); uri u = new uri(fromurl); if (u.iswellformedoriginalstring()) { if (u.isfile) { subdomain = domain = "客户端本地文件路径"; } else { string authority = u.authority; string[] ss = u.authority.split('.'); if (ss.length == 2) { authority = "www." + authority; } int index = authority.indexof('.', 0); domain = authority.substring(index + 1, authority.length - index - 1).replace("comhttp","com"); subdomain = authority.replace("comhttp", "com"); if (ss.length < 2) { domain = "不明路径"; subdomain = "不明路径"; } } } else { if (u.isfile) { subdomain = domain = "客户端本地文件路径"; } else { subdomain = domain = "不明路径"; } } } catch { subdomain = domain = "不明路径"; } } /// <summary> /// 分析 url 字符串中的参数信息。 /// </summary> /// <param name="url">输入的 url</param> /// <param name="baseurl">输出 url 的基础部分</param> /// <param name="nvc">输出分析后得到的 (参数名,参数值) 的集合</param> public static void parseurl(string url, out string baseurl, out namevaluecollection nvc) { if (url == null) throw new argumentnullexception("url"); nvc = new namevaluecollection(); baseurl = ""; if (url == "") return; int questionmarkindex = url.indexof('?'); if (questionmarkindex == -1) { baseurl = url; return; } baseurl = url.substring(0, questionmarkindex); if (questionmarkindex == url.length - 1) return; string ps = url.substring(questionmarkindex + 1); // 开始分析参数对 regex re = new regex(@"(^|&)?(\w+)=([^&]+)(&|$)?", regexoptions.compiled); matchcollection mc = re.matches(ps); foreach (match m in mc) { nvc.add(m.result("$2").tolower(), m.result("$3")); } } #endregion } }
希望本文所述对大家的c#程序设计有所帮助。