URL工具-URLUtil
# 介绍
URL(Uniform Resource Locator)中文名为统一资源定位符,有时也被俗称为网页地址。表示为互联网上的资源,如网页或者FTP地址。
下面是URLUtil
工具类的方法列表,包含了每个方法的说明:
ParseUrl
: 解析URL,返回包含协议、主机名、端口和路径的字符串数组。AddQueryParameters
: 添加查询参数到URL,返回新的URL字符串。RemoveQueryParameters
: 从URL中删除查询参数,返回新的URL字符串。CombineUrls
: 将基础URL和相对URL组合成一个完整的URL,返回组合后的URL字符串。StripQueryAndFragment
: 从URL中删除查询参数和片段,返回不包含查询参数和片段的URL字符串。UrlEncode
: 编码URL字符串,返回编码后的字符串。UrlEncodeQuery
: 编码URL查询字符串,返回编码后的字符串。UrlDecode
: 解码URL字符串,返回解码后的字符串。UrlDecodeQuery
: 解码URL查询字符串,返回解码后的字符串。ExtractDomain
: 从URL中提取域名,返回域名字符串。ExtractPath
: 从URL中提取路径,返回路径字符串。IsHttps
: 判断URL是否为HTTPS协议,返回布尔值。ExtractQueryString
: 从URL中提取查询字符串,返回查询字符串。ExtractFragment
: 从URL中提取片段,返回片段字符串。PathToRelative
: 将URL路径转换为相对路径,返回相对路径字符串。RelativeToPath
: 将相对路径转换为URL路径,返回URL路径字符串。QueryToDictionary
: 将URL的查询参数转换为字典,返回查询参数的键值对字典。
# EasyTool.URLUtil 类
静态工具类 URLUtil,提供了许多方便的方法,用于执行各种URL操作,例如解析URL,拼接URL,编码和解码URL,提取域名和路径,添加或删除查询参数,将URL路径转换为相对路径或URL路径,以及将查询参数转换为字典等。
URLUtil工具类可以帮助开发人员快速轻松地执行各种URL操作,同时确保他们的代码正确和安全。这个工具类还可以减少开发时间和精力,因为许多常见的URL操作已经被包含在这个工具类中了,不需要开发人员再次编写它们。# ParseUrl-解析URL并返回其组成部分
/// <summary>
/// 解析URL并返回其组成部分
/// </summary>
/// <param name="url">要解析的URL。</param>
/// <returns>URL组成部分的字符串数组。</returns>
public static string[] ParseUrl(string url)
# AddQueryParameters-将指定的查询字符串参数添加到URL
/// <summary>
/// 将指定的查询字符串参数添加到URL
/// </summary>
/// <param name="url">要添加查询参数的URL。</param>
/// <param name="parameters">查询参数的键值对。</param>
/// <returns>包含新查询参数的URL。</returns>
public static string AddQueryParameters(string url, params KeyValuePair<string, string>[] parameters)
# RemoveQueryParameters-从URL中删除指定的查询字符串参数
/// <summary>
/// 从URL中删除指定的查询字符串参数
/// </summary>
/// <param name="url">要删除查询参数的URL。</param>
/// <param name="parameters">要删除的查询参数的键。</param>
/// <returns>不包含指定查询参数的URL。</returns>
public static string RemoveQueryParameters(string url, params string[] parameters)
# CombineUrls-将两个URL组合成一个
/// <summary>
/// 将两个URL组合成一个
/// </summary>
/// <param name="baseUrl">基本URL。</param>
/// <param name="relativeUrl">相对于基本URL的URL。</param>
/// <returns>组合后的URL。</returns>
public static string CombineUrls(string baseUrl, string relativeUrl)
# StripQueryAndFragment-从URL中去掉查询参数和片段
/// <summary>
/// 从URL中去掉查询参数和片段
/// </summary>
/// <param name="uri">要去掉查询参数和片段的
/// /// <returns>不包含查询参数和片段的URL。</returns>
private static string StripQueryAndFragment(Uri uri)
# UrlEncode-编码URL字符串
/// <summary>
/// 编码URL字符串
/// </summary>
/// <param name="value">要编码的字符串。</param>
/// <returns>编码后的字符串。</returns>
public static string UrlEncode(string value)
# UrlEncodeQuery-编码URL查询字符串
/// <summary>
/// 编码URL查询字符串
/// </summary>
/// <param name="value">要编码的字符串。</param>
/// <returns>编码后的字符串。</returns>
public static string UrlEncodeQuery(string value)
# UrlDecode-解码URL字符串
/// <summary>
/// 解码URL字符串
/// </summary>
/// <param name="value">要解码的字符串。</param>
/// <returns>解码后的字符串。</returns>
public static string UrlDecode(string value)
# UrlDecodeQuery-解码URL查询字符串
/// <summary>
/// 解码URL查询字符串
/// </summary>
/// <param name="value">要解码的字符串。</param>
/// <returns>解码后的字符串。</returns>
public static string UrlDecodeQuery(string value)
# ExtractDomain-从URL中提取域名
/// <summary>
/// 从URL中提取域名
/// </summary>
/// <param name="url">要提取域名的URL。</param>
/// <returns>URL中的域名。</returns>
public static string ExtractDomain(string url)
# ExtractPath-从URL中提取路径
/// <summary>
/// 从URL中提取路径
/// </summary>
/// <param name="url">要提取路径的URL。</param>
/// <returns>URL中的路径。</returns>
public static string ExtractPath(string url)
# IsHttps-判断URL是否为HTTPS协议
/// <summary>
/// 判断URL是否为HTTPS协议
/// </summary>
/// <param name="url">要判断的URL。</param>
/// <returns>如果是HTTPS协议,则为true,否则为false。</returns>
public static bool IsHttps(string url)
# ExtractQueryString-从URL中提取查询字符串
/// <summary>
/// 从URL中提取查询字符串
/// </summary>
/// <param name="url">要提取查询字符串的URL。</param>
/// <returns>URL中的查询字符串。</returns>
public static string ExtractQueryString(string url)
# ExtractFragment-从URL中提取片段
/// <summary>
/// 从URL中提取片段
/// </summary>
/// <param name="url">要提取片段的URL。</param>
/// <returns>URL中的片段。</returns>
public static string ExtractFragment(string url)
# PathToRelative-将URL路径转换为相对路径
/// <summary>
/// 将URL路径转换为相对路径
/// </summary>
/// <param name="url">要转换的URL路径。</param>
/// <returns>相对于网站根目录的路径。</returns>
public static string PathToRelative(string url)
# RelativeToPath-将相对路径转换为URL路径
/// <summary>
/// 将相对路径转换为URL路径
/// </summary>
/// <param name="relativePath">要转换的相对路径。</param>
/// <param name="baseUrl">基本URL,如果相对路径不是绝对路径,则使用此URL。</param>
/// <returns>URL路径。</returns>
public static string RelativeToPath(string relativePath, string baseUrl)
# QueryToDictionary-将URL的查询参数转换为字典
/// <summary>
/// 将URL的查询参数转换为字典
/// </summary>
/// <param name="url">要提取查询参数的URL。</param>
/// <returns>查询参数的键值对字典。</returns>
public static Dictionary<string, string> QueryToDictionary(string url)
# 使用示例
using System;
class Program
{
static void Main(string[] args)
{
string url = "https://www.example.com/path/page.html?param1=value1¶m2=value2";
// 解析URL
string[] urlParts = URLUtil.ParseUrl(url);
Console.WriteLine("Scheme: {0}", urlParts[0]); // 输出 "https"
Console.WriteLine("Host: {0}", urlParts[1]); // 输出 "www.example.com"
Console.WriteLine("Port: {0}", urlParts[2]); // 输出 "443"
Console.WriteLine("Path: {0}", urlParts[3]); // 输出 "/path/page.html"
// 添加查询参数
string newUrl = URLUtil.AddQueryParameters(url, new KeyValuePair<string, string>("param3", "value3"));
Console.WriteLine("New URL with added query parameter: {0}", newUrl); // 输出 "https://www.example.com/path/page.html?param1=value1¶m2=value2¶m3=value3"
// 删除查询参数
newUrl = URLUtil.RemoveQueryParameters(newUrl, "param1");
Console.WriteLine("New URL with removed query parameter: {0}", newUrl); // 输出 "https://www.example.com/path/page.html?param2=value2¶m3=value3"
// 组合URL
string baseUrl = "https://www.example.com";
string relativeUrl = "/path/page.html";
string combinedUrl = URLUtil.CombineUrls(baseUrl, relativeUrl);
Console.WriteLine("Combined URL: {0}", combinedUrl); // 输出 "https://www.example.com/path/page.html"
// 编码和解码URL
string encodedUrl = URLUtil.UrlEncode(url);
Console.WriteLine("Encoded URL: {0}", encodedUrl); // 输出 "https%3a%2f%2fwww.example.com%2fpath%2fpage.html%3fparam1%3dvalue1%26param2%3dvalue2"
string decodedUrl = URLUtil.UrlDecode(encodedUrl);
Console.WriteLine("Decoded URL: {0}", decodedUrl); // 输出 "https://www.example.com/path/page.html?param1=value1¶m2=value2"
// 提取域名和路径
string domain = URLUtil.ExtractDomain(url);
Console.WriteLine("Domain: {0}", domain); // 输出 "www.example.com"
string path = URLUtil.ExtractPath(url);
Console.WriteLine("Path: {0}", path); // 输出 "/path/page.html"
// 判断是否为HTTPS
bool isHttps = URLUtil.IsHttps(url);
Console.WriteLine("Is HTTPS: {0}", isHttps); // 输出 "True"
}
}
# 输出结果
Scheme: https
Host: www.example.com
Port: 443
Path: /path/page.html
New URL with added query parameter: https://www.example.com/path/page.html?param1=value1¶m2=value2¶m3=value3
New URL with removed query parameter: https://www.example.com/path/page.html?param2=value2¶m3=value3
Combined URL: https://www.example.com/path/page.html
Encoded URL: https%3a%2f%2fwww.example.com%2fpath%2fpage.html%3fparam1%3dvalue1%26param2%3dvalue2
Decoded URL: https://www.example.com/path/page.html?param1=value1¶m2=value2
Domain: www.example.com
Path: /path/page.html
Is HTTPS: True
上次更新: 2023/04/26, 22:10:06