C#中URL的操作類
UrlOper.cs
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#中PDF文件操作類C#
- [C#]C#中字串的操作C#字串
- c# Lambda操作類封裝C#封裝
- C#神器"BlockingCollection"類實現C#神仙操作C#BloCGC
- toLua中Lua呼叫C#中的類C#
- 學習PHP中的URL相關操作函式PHP函式
- JS、C#中URL編碼解碼問題JSC#
- C#中的擴充套件類的理解C#套件
- C#中JSON轉換類C#JSON
- Java中內部類的騷操作Java
- C#操作Microsoft.Office.Interop.Word類庫完整例子C#ROS
- Java中atomic包中的原子操作類總結Java
- JQ操作類與JS操作類的區別JS
- C#獲取URL引數值C#
- Android 外部URL跳轉到APP的操作AndroidAPP
- Java中的13個原子操作類介紹Java
- .net 6 C#中System.IO.Path類的用法C#
- 類操作是什麼意思?jQuery的類操作教程jQuery
- 前端必知必會--操作URL的黑科技前端
- C#操作jsonC#JSON
- C# 字串操作C#字串
- C# 解析獲取Url引數值C#
- [C#]C#時間日期操作C#
- 類操作是什麼意思?jQuery的類操作教程分享jQuery
- C#中誰最快:結構還是類?
- 20220406Java字串操作類中scompareTo()Java字串
- c#中判斷類是否繼承於泛型基類C#繼承泛型
- url中#(hash)的含義
- c#抽象類的定義C#抽象
- C# 操作xml(轉)C#XML
- C# 檔案操作C#
- C#中類的屬性訪問器--get和set分析C#
- 77. C#中的介面和類有什麼異同?C#
- ThinkPHP6中Redis操作服務類的記錄PHPRedis
- 類似優酷 url 的生成是怎麼生成的?
- node中的url常用方法解析
- Dubbo 中的 URL 統一模型模型
- SAP Spartacus 中的 HTTP URL parametersHTTP