Uri 类

定义

提供统一资源标识符 (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 : MarshalByRefObject, System::Runtime::Serialization::ISerializable
public class Uri
public class Uri : System.Runtime.Serialization.ISerializable
public class 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 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
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)
已过时.

UriSerializationInfo 类的指定实例初始化 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)

根据指定的基 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

获取用问号 (?) 分隔的 AbsolutePathQuery 属性。

Port

获取此 URI 的端口号。

Query

获取指定 URI 中包含的任何查询信息,包括前导“?”字符(如果不是空)。

Scheme

获取此 URI 的方案名称。

Segments

获取包含构成指定 URI 的路径段的数组。

UserEscaped

获取一个值,该值指示 URI 字符串是否在创建 Uri 实例之前已被完全转义。

UserInfo

获取用户名、密码或其他与指定 URI 关联的特定于用户的信息。

方法

Canonicalize()
已过时.
已过时.
已过时.

将内部存储的 URI 转换为规范化格式。

CheckHostName(String)

确定指定的主机名是否为有效的 DNS 名称。

CheckSchemeName(String)

确定指定的方案名是否有效。

CheckSecurity()
已过时.
已过时.
已过时.

调用此方法不起任何作用。

Compare(Uri, Uri, UriComponents, UriFormat, StringComparison)

使用指定的比较规则比较两个 URI 的指定部分。

CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。

(继承自 MarshalByRefObject)
Equals(Object)

比较两个 Uri 实例是否相等。

Escape()
已过时.
已过时.
已过时.

将路径部分中的任何不安全字符或保留字符转换为对应的十六进制字符表示形式。

EscapeDataString(ReadOnlySpan<Char>)

提供统一资源标识符 (URI) 的对象表示形式和对 URI 各部分的轻松访问。

EscapeDataString(String)

将字符串转换为它的转义表示形式。

EscapeString(String)
已过时.
已过时.
已过时.
已过时.

将字符串转换为它的转义表示形式。

EscapeUriString(String)
已过时.
已过时.

将 URI 字符串转换为它的转义表示形式。

FromHex(Char)

获取十六进制数字的十进制值。

GetComponents(UriComponents, UriFormat)

使用特殊字符的指定转义,获取当前实例的指定部分。

GetHashCode()

获取 URI 的哈希代码。

GetLeftPart(UriPartial)

获取 Uri 实例的指定部分。

GetLifetimeService()
已过时.

检索控制此实例的生存期策略的当前生存期服务对象。

(继承自 MarshalByRefObject)
GetObjectData(SerializationInfo, StreamingContext)

返回序列化当前实例所需的数据。

GetType()

获取当前实例的 Type

(继承自 Object)
HexEscape(Char)

将指定的字符转换为它的等效十六进制字符。

HexUnescape(String, Int32)

将字符的指定十六进制表示形式转换为字符。

InitializeLifetimeService()
已过时.

获取生存期服务对象来控制此实例的生存期策略。

(继承自 MarshalByRefObject)
IsBadFileSystemCharacter(Char)
已过时.
已过时.
已过时.

指示某个字符在文件系统名称中是否无效。

IsBaseOf(Uri)

确定当前的 Uri 实例是否为指定 Uri 实例的基。

IsExcludedCharacter(Char)
已过时.
已过时.
已过时.

确定是否应转义指定的字符。

IsHexDigit(Char)

确定指定的字符是否为有效的十六进制数字。

IsHexEncoding(String, Int32)

确定字符串中的一个字符是否为十六进制编码。

IsReservedCharacter(Char)
已过时.
已过时.
已过时.

确定指定的字符是否为保留字符。

IsWellFormedOriginalString()

指示用于构造此 Uri 的字符串是否格式良好,以及它是否不需要进一步转义。

IsWellFormedUriString(String, UriKind)

通过尝试用字符串构造一个 URI 来指示字符串是否为格式良好的,并确保字符串不需要进一步转义。

MakeRelative(Uri)
已过时.
已过时.
已过时.
已过时.

确定两个 Uri 实例之间的差异。

MakeRelativeUri(Uri)

确定两个 Uri 实例之间的差异。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。

(继承自 MarshalByRefObject)
Parse()
已过时.
已过时.
已过时.

分析当前实例的 URI 以确保它包含有效 URI 所需的所有部分。

ToString()

获取指定的 Uri 实例的规范化字符串表示形式。

TryCreate(String, UriCreationOptions, Uri)

Uri使用指定的 String 实例和 UriCreationOptions创建新的 。

TryCreate(String, UriKind, Uri)

使用指定的 Uri 实例和 String 创建一个新的 UriKind

TryCreate(Uri, String, Uri)

使用指定基和相对 Uri 实例创建一个新的 String

TryCreate(Uri, Uri, Uri)

使用指定基和相对 Uri 实例创建一个新的 Uri

TryEscapeDataString(ReadOnlySpan<Char>, Span<Char>, Int32)

提供统一资源标识符 (URI) 的对象表示形式和对 URI 各部分的轻松访问。

TryFormat(Span<Char>, Int32)

尝试将实例的规范字符串表示形式 Uri 格式化为指定的范围。

TryUnescapeDataString(ReadOnlySpan<Char>, Span<Char>, Int32)

提供统一资源标识符 (URI) 的对象表示形式和对 URI 各部分的轻松访问。

Unescape(String)
已过时.
已过时.
已过时.

转换指定的字符串,方法是将任何转义序列替换为相应的未转义表示形式。

UnescapeDataString(ReadOnlySpan<Char>)

提供统一资源标识符 (URI) 的对象表示形式和对 URI 各部分的轻松访问。

UnescapeDataString(String)

将字符串转换为它的非转义表示形式。

运算符

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 都是线程安全的,可以从多个线程同时使用。

另请参阅