C#自定义针对URL地址的处理类实例
作者:feige 时间:2022-09-12 16:54:02
本文实例讲述了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#程序设计有所帮助。
标签:C#,URL,类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android Java try catch 失效问题及解决
2023-06-17 17:07:33
C# 一个WCF简单实例
2023-07-12 20:53:43
![](https://img.aspxhome.com/file/2023/5/66125_0s.jpg)
Java AWT中常用的三种布局管理器详解
2023-02-11 20:55:25
![](https://img.aspxhome.com/file/2023/6/65586_0s.png)
JavaCV实战之调用摄像头基础详解
2022-07-15 02:14:10
![](https://img.aspxhome.com/file/2023/5/66625_0s.png)
Java编程BigDecimal用法实例分享
2022-05-02 05:40:06
SpringBoot动态修改日志级别的操作
2022-10-31 05:00:30
![](https://img.aspxhome.com/file/2023/6/67116_0s.png)
Mybatis-Plus的多数据源你了解吗
2023-07-22 00:46:59
![](https://img.aspxhome.com/file/2023/7/57617_0s.png)
Mac OS下为Android Studio编译FFmpeg解码库的详细教程
2023-06-30 02:37:54
Mybatis关于动态排序 #{} ${}问题
2023-09-01 17:34:57
Android 如何获取手机总内存和可用内存等信息
2023-07-27 13:11:42
java中的key接口解析
2023-09-22 14:44:00
springboot创建多module项目的实例
2021-09-09 20:13:20
![](https://img.aspxhome.com/file/2023/0/63390_0s.png)
SpringCloud Feign转发请求头(防止session失效)的解决方案
2022-08-29 12:25:59
C#日期格式字符串的相互转换操作实例分析
2021-09-01 10:06:56
分布式系统下调用链追踪技术面试题
2023-11-25 05:55:00
![](https://img.aspxhome.com/file/2023/6/60126_0s.png)
SpringBoot如何集成PageHelper分页功能
2022-10-22 03:18:34
详解Java实践之建造者模式
2023-01-14 23:03:13
![](https://img.aspxhome.com/file/2023/1/65191_0s.jpg)
Java实现简易学生管理系统
2022-10-16 19:26:49
![](https://img.aspxhome.com/file/2023/1/69301_0s.jpg)
使用Mybatis-Plus时的SqlSessionFactory问题及处理
2022-01-30 07:40:04
JDK源码中一些实用的“小技巧”总结
2022-10-21 08:17:50
![](https://img.aspxhome.com/file/2023/1/68161_0s.jpg)