C#中UriComponents枚舉的實現(xiàn)示例
本篇文章來分享一下C#的UriComponents枚舉,UriComponents是C#中的一個枚舉類型,位于System命名空間下,用于指定從Uri對象中提取哪些部分的信息,常見于處理統(tǒng)一資源標識符(URI),例如在進行網(wǎng)絡編程、URL解析等場景中。
1.UriComponents枚舉的常用成員
枚舉成員 | 含義描述 | 適用場景 |
SerializationInfoString | 內(nèi)部用于URI序列化的標記,外部調(diào)用無實際意義(值為int.MinValue)。 | 框架內(nèi)部序列化邏輯,開發(fā)者無需使用。 |
Scheme | 提取URI的協(xié)議部分(如http、https、ftp)。 | 1.判斷請求協(xié)議類型(如是否為加密的https); 2.根據(jù)協(xié)議選擇不同的處理邏輯(如ftp需登錄)。 |
UserInfo | 提取URI中的用戶認證信息(格式:用戶名:密碼,如user:password)。 | 1.解析包含身份認證的舊版URI; 2.日志記錄中提取認證信息(需注意安全風險)。 |
Host | 提取主機名或IP地址(如example.com、192.168.1.1),不含端口。 | 1.驗證服務器域名(如白名單校驗);2.統(tǒng)計訪問的主機分布。 |
Port | 提取端口號(如8080),默認端口(如https的443)返回-1。 | 1.檢查是否使用非默認端口(如8080需特殊處理); 2.構(gòu)建網(wǎng)絡連接時指定端口。 |
SchemeAndServer | 組合值,提取協(xié)議+用戶信息+主機+端口(如https://user@example.com:8080)。 | 1.獲取服務的基礎(chǔ)訪問地址(如拼接新路徑); 2.跨域請求時驗證服務器標識。 |
Path | 提取資源路徑(如/api/users),不含查詢參數(shù)或片段。 | 1.解析API路由(如/api/users對應用戶模塊); 2.驗證請求路徑的合法性(防止越權(quán))。 |
Query | 提取查詢字符串(如id=123&name=test),不含?分隔符。 | 1.解析URL中的請求參數(shù)(如分頁、篩選條件); 2.日志記錄或分析用戶請求參數(shù)。 |
PathAndQuery | 組合值,提取路徑+查詢字符串(如/api/users?id=123)。 | 1.構(gòu)建帶參數(shù)的資源路徑; 2.比較兩個請求的路徑和參數(shù)是否一致。 |
HttpRequestUrl | 組合值,提取HTTP請求完整路徑(不含片段,如https://example.com/api?id=1)。 | 1.生成HTTP請求的目標URL; 2.記錄完整的請求地址(不含頁面內(nèi)錨點)。 |
Fragment | 提取片段標識符(如section1),即#后的內(nèi)容(不含#)。 | 1.網(wǎng)頁內(nèi)錨點定位(如滾動到#chapter2章節(jié)); 2.單頁應用(SPA)的路由跳轉(zhuǎn)。 |
AbsoluteUri | 組合值,提取完整URI(包含所有部分)。 | 1.完整復制或記錄URI; 2.對URI進行整體格式化或編碼轉(zhuǎn)換。 |
StrongPort | 強制返回實際端口號(即使是默認端口,如https會返回443,而非-1)。 | 1.需要顯式展示默認端口的場景(如文檔生成); 2.端口相關(guān)的精確校驗邏輯。 |
HostAndPort | 組合值,提取主機+顯式端口(如example.com:443,含默認端口)。 | 1.展示服務器的主機和端口信息(強制顯式端口); 2.比較兩個URI的主機和端口是否一致。 |
StrongAuthority | 組合值,提取用戶信息+主機+顯式端口(如user:pass@example.com:443)。 | 1.需完整展示認證信息+主機端口的場景; 2.舊版系統(tǒng)的身份驗證相關(guān)邏輯。 |
NormalizedHost | 提取標準化主機名(如大寫轉(zhuǎn)小寫EXAMPLE.COM→example.com,處理國際化域名)。 | 1.域名比較(忽略大小寫差異); 2.處理國際化域名(如中文域名的Punycode轉(zhuǎn)換)。 |
KeepDelimiter | 保留分隔符(如?、#),需與其他成員組合使用(如QueryKeepDelimiter返回?id=123)。 | 1.需要保留原始分隔符的場景(如URL格式化展示); 2.生成帶分隔符的查詢字符串或片段。 |
2.使用示例
以"https://user:password@example.com:8080/api/users?id=123#section1"為例,是一個完整的URI(統(tǒng)一資源標識符),首先來了解一下各部分含義:
了解完整的URI
組成部分 | 示例值 | 說明 |
協(xié)議(Scheme) | https | 通信協(xié)議,https表示加密的HTTP協(xié)議(默認端口443)。 |
用戶信息 | user:password | 登錄認證信息(格式:用戶名:密碼),現(xiàn)代應用中較少使用(不安全)。 |
主機名(Host) | example.com | 服務器域名或IP地址,標識資源所在的服務器。 |
端口(Port) | 8080 | 服務器端口號,此處為非默認端口(默認端口可省略,如https默認443)。 |
路徑(Path) | /api/users | 資源在服務器上的路徑,通常對應后端接口或文件位置。 |
查詢字符串 | id=123 | 附加參數(shù)(格式:key=value&key2=value2),用于篩選或配置資源。 |
片段(Fragment) | section1 | 頁面內(nèi)錨點,用于定位頁面中的具體位置(如網(wǎng)頁中的章節(jié))。 |
測試代碼
using System;
using UnityEngine;
public class UriComponentsTest : MonoBehaviour
{
private void Start()
{
string uriString = "https://user:password@example.com:8080/api/users?id=123#section1";
Uri uri = new Uri(uriString);
UriFormat format = UriFormat.Unescaped;//不轉(zhuǎn)義特殊字符
//1. SerializationInfoString:內(nèi)部序列化使用(外部調(diào)用無實際意義)
string serializationInfo = uri.GetComponents(UriComponents.SerializationInfoString, format);
Debug.Log($"SerializationInfoString: {serializationInfo}");
//輸出:SerializationInfoString: https://user:password@example.com:8080/api/users?id=123#section1
//2. Scheme:提取協(xié)議部分
string scheme = uri.GetComponents(UriComponents.Scheme, format);
Debug.Log($"Scheme: {scheme}");
//輸出:Scheme: https
//3. UserInfo:提取用戶信息(用戶名:密碼)
string userInfo = uri.GetComponents(UriComponents.UserInfo, format);
Debug.Log($"UserInfo: {userInfo}");
//輸出:UserInfo: user:password
//4. Host:提取主機名
string host = uri.GetComponents(UriComponents.Host, format);
Debug.Log($"Host: {host}");
//輸出:Host: example.com
//5. Port:提取端口號(默認端口返回-1)
string port = uri.GetComponents(UriComponents.Port, format);
Debug.Log($"Port: {port}");
//輸出:Port: 8080
//6. SchemeAndServer:協(xié)議+用戶信息+主機+端口(1|2|4|8=13)
string schemeAndServer = uri.GetComponents(UriComponents.SchemeAndServer, format);
Debug.Log($"SchemeAndServer: {schemeAndServer}");
//輸出:SchemeAndServer: https://user:password@example.com:8080
//7. Path:提取路徑部分
string path = uri.GetComponents(UriComponents.Path, format);
Debug.Log($"Path: {path}");
//輸出:Path: /api/users
//8. Query:提取查詢字符串(不含?)
string query = uri.GetComponents(UriComponents.Query, format);
Debug.Log($"Query: {query}");
//輸出:Query: id=123
//9. PathAndQuery:路徑+查詢字符串(16|32=48)
string pathAndQuery = uri.GetComponents(UriComponents.PathAndQuery, format);
Debug.Log($"PathAndQuery: {pathAndQuery}");
//輸出:PathAndQuery: /api/users?id=123
//10. HttpRequestUrl:HTTP請求完整路徑(13|48=61)
string httpRequestUrl = uri.GetComponents(UriComponents.HttpRequestUrl, format);
Debug.Log($"HttpRequestUrl: {httpRequestUrl}");
//輸出:HttpRequestUrl: https://user:password@example.com:8080/api/users?id=123
//11. Fragment:提取片段(錨點,不含#)
string fragment = uri.GetComponents(UriComponents.Fragment, format);
Debug.Log($"Fragment: {fragment}");
//輸出:Fragment: section1
//12. AbsoluteUri:完整URI(13|48|64=127)
string absoluteUri = uri.GetComponents(UriComponents.AbsoluteUri, format);
Debug.Log($"AbsoluteUri: {absoluteUri}");
//輸出:AbsoluteUri: https://user:password@example.com:8080/api/users?id=123#section1
//13. StrongPort:強制返回實際端口(包括默認端口)
string strongPort = uri.GetComponents(UriComponents.StrongPort, format);
Debug.Log($"StrongPort: {strongPort}");
//輸出:StrongPort: 8080
//14. HostAndPort:主機+顯式端口(4|8|128=132)
string hostAndPort = uri.GetComponents(UriComponents.HostAndPort, format);
Debug.Log($"HostAndPort: {hostAndPort}");
//輸出:HostAndPort: example.com:8080
//15. StrongAuthority:用戶信息+主機+顯式端口(2|132=134)
string strongAuthority = uri.GetComponents(UriComponents.StrongAuthority, format);
Debug.Log($"StrongAuthority: {strongAuthority}");
//輸出:StrongAuthority: user:password@example.com:8080
//16. NormalizedHost:標準化主機名(小寫+IDN處理)
string normalizedHost = uri.GetComponents(UriComponents.NormalizedHost, format);
Debug.Log($"NormalizedHost: {normalizedHost}");
//輸出:NormalizedHost: example.com
//17. KeepDelimiter:保留?分隔符(需與其他成員組合)
string queryWithDelimiter = uri.GetComponents(UriComponents.Query | UriComponents.KeepDelimiter, format);
Debug.Log($"Query | KeepDelimiter: {queryWithDelimiter}");
//輸出:Query | KeepDelimiter: ?id=123
}
}
結(jié)果

3.總結(jié)
UriComponents枚舉的核心價值是精確提取URI的特定部分,避免手動切割字符串可能導致的錯誤(如特殊字符處理、格式差異)。在網(wǎng)絡請求、URL解析、路由處理等場景中,使用它能讓代碼更簡潔、可靠。
到此這篇關(guān)于C#中UriComponents枚舉的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)C# UriComponents枚舉內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#實現(xiàn)win10 uwp 右擊浮出窗在點擊位置
本文主要讓MenuFlyout出現(xiàn)在我們右擊位置。我們建一個ListView,然后綁定后臺,在我們ListView要右擊顯示我們的浮出,要求我們的浮出在我們點擊位置2016-10-10

