Uri 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供统一资源标识符(URI)的对象表示形式,并轻松访问 URI 的各个部分。
public ref class Uripublic ref class Uri : System::Runtime::Serialization::ISerializablepublic ref class Uri : ISpanFormattable, System::Runtime::Serialization::ISerializablepublic ref class Uri : IEquatable<Uri ^>, ISpanFormattable, System::Runtime::Serialization::ISerializablepublic ref class Uri : MarshalByRefObject, System::Runtime::Serialization::ISerializablepublic class Uripublic class Uri : System.Runtime.Serialization.ISerializablepublic class Uri : ISpanFormattable, System.Runtime.Serialization.ISerializablepublic class Uri : IEquatable<Uri>, ISpanFormattable, System.Runtime.Serialization.ISerializable[System.Serializable]
public class Uri : MarshalByRefObject, System.Runtime.Serialization.ISerializable[System.Serializable]
[System.ComponentModel.TypeConverter(typeof(System.UriTypeConverter))]
public class Uri : System.Runtime.Serialization.ISerializabletype Uri = classtype Uri = class
    interface ISerializabletype Uri = class
    interface ISpanFormattable
    interface IFormattable
    interface ISerializabletype Uri = class
    interface IFormattable
    interface ISpanFormattable
    interface IEquatable<Uri>
    interface ISerializable[<System.Serializable>]
type Uri = class
    inherit MarshalByRefObject
    interface ISerializable[<System.Serializable>]
[<System.ComponentModel.TypeConverter(typeof(System.UriTypeConverter))>]
type Uri = class
    interface ISerializablePublic Class UriPublic Class Uri
Implements ISerializablePublic Class Uri
Implements ISerializable, ISpanFormattablePublic Class Uri
Implements IEquatable(Of Uri), ISerializable, ISpanFormattablePublic Class Uri
Inherits MarshalByRefObject
Implements ISerializable- 继承
- 
				Uri
- 继承
- 属性
- 实现
示例
以下示例创建 Uri 类的实例,并使用它通过 HttpClient执行 GET 请求。
Uri^ siteUri = gcnew Uri("http://www.contoso.com/");
// HttpClient lifecycle management best practices:
// https://free.blessedness.top/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use
HttpClient^ client = gcnew HttpClient;
HttpRequestMessage^ request = gcnew HttpRequestMessage(HttpMethod::Get, siteUri);
HttpResponseMessage^ response = client->Send(request);
Uri siteUri = new Uri("http://www.contoso.com/");
// HttpClient lifecycle management best practices:
// https://free.blessedness.top/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, siteUri);
HttpResponseMessage response = client.Send(request);
let siteUri = Uri "http://www.contoso.com/"
// HttpClient lifecycle management best practices:
// https://free.blessedness.top/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use
use client = new HttpClient ()
use request = new HttpRequestMessage (HttpMethod.Get, siteUri)
use response = client.Send request
Dim siteUri As New Uri("http://www.contoso.com/")
' HttpClient lifecycle management best practices:
' https://free.blessedness.top/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use
Dim client As New HttpClient()
Dim request As New HttpRequestMessage(HttpMethod.Get, siteUri)
Dim response As HttpResponseMessage = client.Send(request)
以下代码片段显示了类上各种属性的示例值。
Uri uri = new Uri("https://user:password@www.contoso.com:80/Home/Index.htm?q1=v1&q2=v2#FragmentName");
Console.WriteLine($"AbsolutePath: {uri.AbsolutePath}");
Console.WriteLine($"AbsoluteUri: {uri.AbsoluteUri}");
Console.WriteLine($"DnsSafeHost: {uri.DnsSafeHost}");
Console.WriteLine($"Fragment: {uri.Fragment}");
Console.WriteLine($"Host: {uri.Host}");
Console.WriteLine($"HostNameType: {uri.HostNameType}");
Console.WriteLine($"IdnHost: {uri.IdnHost}");
Console.WriteLine($"IsAbsoluteUri: {uri.IsAbsoluteUri}");
Console.WriteLine($"IsDefaultPort: {uri.IsDefaultPort}");
Console.WriteLine($"IsFile: {uri.IsFile}");
Console.WriteLine($"IsLoopback: {uri.IsLoopback}");
Console.WriteLine($"IsUnc: {uri.IsUnc}");
Console.WriteLine($"LocalPath: {uri.LocalPath}");
Console.WriteLine($"OriginalString: {uri.OriginalString}");
Console.WriteLine($"PathAndQuery: {uri.PathAndQuery}");
Console.WriteLine($"Port: {uri.Port}");
Console.WriteLine($"Query: {uri.Query}");
Console.WriteLine($"Scheme: {uri.Scheme}");
Console.WriteLine($"Segments: {string.Join(", ", uri.Segments)}");
Console.WriteLine($"UserEscaped: {uri.UserEscaped}");
Console.WriteLine($"UserInfo: {uri.UserInfo}");
// AbsolutePath: /Home/Index.htm
// AbsoluteUri: https://user:password@www.contoso.com:80/Home/Index.htm?q1=v1&q2=v2#FragmentName
// DnsSafeHost: www.contoso.com
// Fragment: #FragmentName
// Host: www.contoso.com
// HostNameType: Dns
// IdnHost: www.contoso.com
// IsAbsoluteUri: True
// IsDefaultPort: False
// IsFile: False
// IsLoopback: False
// IsUnc: False
// LocalPath: /Home/Index.htm
// OriginalString: https://user:password@www.contoso.com:80/Home/Index.htm?q1=v1&q2=v2#FragmentName
// PathAndQuery: /Home/Index.htm?q1=v1&q2=v2
// Port: 80
// Query: ?q1=v1&q2=v2
// Scheme: https
// Segments: /, Home/, Index.htm
// UserEscaped: False
// UserInfo: user:password
let uri = Uri "https://user:password@www.contoso.com:80/Home/Index.htm?q1=v1&q2=v2#FragmentName"
printfn $"AbsolutePath: {uri.AbsolutePath}"
printfn $"AbsoluteUri: {uri.AbsoluteUri}"
printfn $"DnsSafeHost: {uri.DnsSafeHost}"
printfn $"Fragment: {uri.Fragment}"
printfn $"Host: {uri.Host}"
printfn $"HostNameType: {uri.HostNameType}"
printfn $"IdnHost: {uri.IdnHost}"
printfn $"IsAbsoluteUri: {uri.IsAbsoluteUri}"
printfn $"IsDefaultPort: {uri.IsDefaultPort}"
printfn $"IsFile: {uri.IsFile}"
printfn $"IsLoopback: {uri.IsLoopback}"
printfn $"IsUnc: {uri.IsUnc}"
printfn $"LocalPath: {uri.LocalPath}"
printfn $"OriginalString: {uri.OriginalString}"
printfn $"PathAndQuery: {uri.PathAndQuery}"
printfn $"Port: {uri.Port}"
printfn $"Query: {uri.Query}"
printfn $"Scheme: {uri.Scheme}"
printfn $"""Segments: {String.Join(", ", uri.Segments)}"""
printfn $"UserEscaped: {uri.UserEscaped}"
printfn $"UserInfo: {uri.UserInfo}"
// AbsolutePath: /Home/Index.htm
// AbsoluteUri: https://user:password@www.contoso.com:80/Home/Index.htm?q1=v1&q2=v2#FragmentName
// DnsSafeHost: www.contoso.com
// Fragment: #FragmentName
// Host: www.contoso.com
// HostNameType: Dns
// IdnHost: www.contoso.com
// IsAbsoluteUri: True
// IsDefaultPort: False
// IsFile: False
// IsLoopback: False
// IsUnc: False
// LocalPath: /Home/Index.htm
// OriginalString: https://user:password@www.contoso.com:80/Home/Index.htm?q1=v1&q2=v2#FragmentName
// PathAndQuery: /Home/Index.htm?q1=v1&q2=v2
// Port: 80
// Query: ?q1=v1&q2=v2
// Scheme: https
// Segments: /, Home/, Index.htm
// UserEscaped: False
// UserInfo: user:password
注解
有关此 API 的详细信息,请参阅 uri
构造函数
| Uri(SerializationInfo, StreamingContext) | 
				已过时.
			 从 SerializationInfo 和 StreamingContext 类的指定实例初始化 Uri 类的新实例。 | 
| Uri(String) | 使用指定的 URI 初始化 Uri 类的新实例。 | 
| Uri(String, Boolean) | 
				已过时.
			 
				已过时.
			 
				已过时.
			 使用指定的 URI 初始化 Uri 类的新实例,并显式控制字符转义。 | 
| Uri(String, UriCreationOptions) | 使用指定的 URI 和其他 UriCreationOptions初始化 Uri 类的新实例。 | 
| Uri(String, UriKind) | 使用指定的 URI 初始化 Uri 类的新实例。 使用此构造函数可以指定 URI 字符串是相对 URI、绝对 URI 还是不确定。 | 
| Uri(Uri, String) | 根据指定的基 URI 和相对 URI 字符串初始化 Uri 类的新实例。 | 
| Uri(Uri, String, Boolean) | 
				已过时.
			 
				已过时.
			 
				已过时.
			 根据指定的基和相对 URI 初始化 Uri 类的新实例,并显式控制字符转义。 | 
| Uri(Uri, Uri) | 
字段
| SchemeDelimiter | 指定将通信协议方案与 URI 的地址部分分开的字符。 此字段为只读。 | 
| UriSchemeFile | 指定 URI 是指向文件的指针。 此字段为只读。 | 
| UriSchemeFtp | 指定通过文件传输协议(FTP)访问 URI。 此字段为只读。 | 
| UriSchemeFtps | 指定通过文件传输协议安全(FTPS)访问 URI。 此字段为只读。 | 
| UriSchemeGopher | 指定通过 Gopher 协议访问 URI。 此字段为只读。 | 
| UriSchemeHttp | 指定通过超文本传输协议(HTTP)访问 URI。 此字段为只读。 | 
| UriSchemeHttps | 指定通过安全超文本传输协议(HTTPS)访问 URI。 此字段为只读。 | 
| UriSchemeMailto | 指定 URI 是电子邮件地址,并通过简单邮件传输协议(SMTP)进行访问。 此字段为只读。 | 
| UriSchemeNetPipe | 指定通过 Windows Communication Foundation (WCF)使用的 NetPipe 方案访问 URI。 此字段为只读。 | 
| UriSchemeNetTcp | 指定通过 Windows Communication Foundation 使用的 NetTcp 方案(WCF)访问 URI。 此字段为只读。 | 
| UriSchemeNews | 指定 URI 是 Internet 新闻组,并通过网络新闻传输协议(NNTP)进行访问。 此字段为只读。 | 
| UriSchemeNntp | 指定 URI 是 Internet 新闻组,并通过网络新闻传输协议(NNTP)进行访问。 此字段为只读。 | 
| UriSchemeSftp | 指定通过 SSH 文件传输协议(SFTP)访问 URI。 此字段为只读。 | 
| UriSchemeSsh | 指定通过安全套接字外壳协议(SSH)访问 URI。 此字段为只读。 | 
| UriSchemeTelnet | 指定通过 Telnet 协议访问 URI。 此字段为只读。 | 
| UriSchemeWs | 指定通过 WebSocket 协议 (WS) 访问 URI。 此字段为只读。 | 
| UriSchemeWss | 指定通过 WebSocket 安全协议 (WSS) 访问 URI。 此字段为只读。 | 
属性
| AbsolutePath | 获取 URI 的绝对路径。 | 
| AbsoluteUri | 获取绝对 URI。 | 
| Authority | 获取域名系统(DNS)主机名或 IP 地址以及服务器的端口号。 | 
| DnsSafeHost | 获取一个主机名,该主机名(如有必要)可以安全地用于 DNS 解析。 | 
| Fragment | 获取转义 URI 片段,包括前导“#”字符(如果不是空)。 | 
| Host | 获取此实例的主机组件。 | 
| HostNameType | 获取 URI 中指定的主机名的类型。 | 
| IdnHost | 根据需要使用 Punycode 获取主机符合 RFC 3490 的国际域名。 如有必要,此字符串在未转义后可以安全地用于 DNS 解析。 | 
| IsAbsoluteUri | 获取一个值,该值指示 Uri 实例是否为绝对实例。 | 
| IsDefaultPort | 获取一个值,该值指示 URI 的端口值是否为此方案的默认值。 | 
| IsFile | 获取一个值,该值指示指定的 Uri 是否为文件 URI。 | 
| IsLoopback | 获取一个值,该值指示指定的 Uri 是否引用本地主机。 | 
| IsUnc | 获取一个值,该值指示指定的 Uri 是否为通用命名约定 (UNC) 路径。 | 
| LocalPath | 获取文件名的本地操作系统表示形式。 | 
| OriginalString | 获取传递给 Uri 构造函数的原始 URI 字符串。 | 
| PathAndQuery | 获取用问号(?)分隔的 AbsolutePath 和 Query 属性。 | 
| Port | 获取此 URI 的端口号。 | 
| Query | 获取指定 URI 中包含的任何查询信息,包括前导“?”字符(如果不是空)。 | 
| Scheme | 获取此 URI 的方案名称。 | 
| Segments | 获取一个数组,其中包含构成指定 URI 的路径段。 | 
| UserEscaped | 获取一个值,该值指示在创建 Uri 实例之前是否完全转义 URI 字符串。 | 
| UserInfo | 获取与指定 URI 关联的用户名、密码或其他特定于用户的信息。 | 
方法
运算符
| Equality(Uri, Uri) | 确定两个 Uri 实例是否具有相同的值。 | 
| Inequality(Uri, Uri) | 确定两个 Uri 实例是否没有相同的值。 | 
显式接口实现
| IFormattable.ToString(String, IFormatProvider) | 使用指定格式设置当前实例的值的格式。 | 
| ISerializable.GetObjectData(SerializationInfo, StreamingContext) | 返回序列化当前实例所需的数据。 | 
| ISpanFormattable.TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider) | 尝试将当前实例的值格式化为提供的字符范围。 | 
适用于
线程安全性
Uri 的所有成员都是线程安全的,可以从多个线程并发使用。
另请参阅
- IdnElement
- IriParsingElement
- UriSection
- DnsSafeHost
- MakeRelative(Uri)
- IsWellFormedOriginalString()
- UriBuilder
- 版本 2.0 中对 System.Uri 命名空间的更改
- system.Uri.Uri 中的 国际资源标识符支持 
- .NET Framework 中的 网络编程