Uri 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供统一资源标识符 (URI) 的对象表示形式和对 URI 各部分的轻松访问。
public ref class Uri
public ref class Uri : System::Runtime::Serialization::ISerializable
public ref class Uri : ISpanFormattable, System::Runtime::Serialization::ISerializable
public ref class Uri : IEquatable<Uri ^>, ISpanFormattable, System::Runtime::Serialization::ISerializable
public ref class Uri : MarshalByRefObject, System::Runtime::Serialization::ISerializable
public class Uri
public class Uri : System.Runtime.Serialization.ISerializable
public class Uri : ISpanFormattable, System.Runtime.Serialization.ISerializable
public 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.ISerializable
type Uri = class
type Uri = class
interface ISerializable
type Uri = class
interface ISpanFormattable
interface IFormattable
interface ISerializable
type 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 ISerializable
Public Class Uri
Public Class Uri
Implements ISerializable
Public Class Uri
Implements ISerializable, ISpanFormattable
Public Class Uri
Implements IEquatable(Of Uri), ISerializable, ISpanFormattable
Public Class Uri
Inherits MarshalByRefObject
Implements ISerializable
- 继承
-
Uri
- 继承
- 属性
- 实现
示例
以下示例创建 类的实例, Uri 并使用它对 执行 GET 请求 HttpClient。
Uri^ siteUri = gcnew Uri("http://www.contoso.com/");
// HttpClient lifecycle management best practices:
// https://learn.microsoft.com/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://learn.microsoft.com/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://learn.microsoft.com/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://learn.microsoft.com/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 的补充 API 说明。
构造函数
Uri(SerializationInfo, StreamingContext) |
已过时.
从 Uri 和 SerializationInfo 类的指定实例初始化 StreamingContext 类的新实例。 |
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, 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 (WCF) 使用的 NetTcp 方案访问该 URI。 此字段为只读。 |
UriSchemeNews |
指定 URI 是 Internet 新闻组,而且可以通过 Network 新闻传输协议 (NNTP) 进行访问。 此字段为只读。 |
UriSchemeNntp |
指定 URI 是 Internet 新闻组,而且可以通过 Network 新闻传输协议 (NNTP) 进行访问。 此字段为只读。 |
UriSchemeSftp |
指定通过 SSH 文件传输协议 (SFTP) 访问 URI。 此字段为只读。 |
UriSchemeSsh |
指定通过安全套接字外壳协议访问 URI (SSH) 。 此字段为只读。 |
UriSchemeTelnet |
指定通过 Telnet 协议访问 URI。 此字段为只读。 |
UriSchemeWs |
指定通过 WebSocket 协议 (WS) 访问 URI。 此字段为只读。 |
UriSchemeWss |
指定通过 WebSocket Secure 协议 (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 都是线程安全的,可以从多个线程同时使用。
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈