HttpListenerRequest.Url 属性

定义

获取客户端请求的 Uri 对象。

public:
 property Uri ^ Url { Uri ^ get(); };
public Uri? Url { get; }
public Uri Url { get; }
member this.Url : Uri
Public ReadOnly Property Url As Uri

属性值

Uri

一个 Uri 对象,用于标识客户端请求的资源。

示例

下面的代码示例演示如何使用 Url 属性。

public static void ShowRequestProperties1 (HttpListenerRequest request)
{
    // Display the MIME types that can be used in the response.
    string[] types = request.AcceptTypes;
    if (types != null)
    {
        Console.WriteLine("Acceptable MIME types:");
        foreach (string s in types)
        {
            Console.WriteLine(s);
        }
    }
    // Display the language preferences for the response.
    types = request.UserLanguages;
    if (types != null)
    {
        Console.WriteLine("Acceptable natural languages:");
        foreach (string l in types)
        {
            Console.WriteLine(l);
        }
    }

    // Display the URL used by the client.
    Console.WriteLine("URL: {0}", request.Url.OriginalString);
    Console.WriteLine("Raw URL: {0}", request.RawUrl);
    Console.WriteLine("Query: {0}", request.QueryString);

    // Display the referring URI.
    Console.WriteLine("Referred by: {0}", request.UrlReferrer);

    //Display the HTTP method.
    Console.WriteLine("HTTP Method: {0}", request.HttpMethod);
    //Display the host information specified by the client;
    Console.WriteLine("Host name: {0}", request.UserHostName);
    Console.WriteLine("Host address: {0}", request.UserHostAddress);
    Console.WriteLine("User agent: {0}", request.UserAgent);
}
Public Shared Sub ShowRequestProperties1(ByVal request As HttpListenerRequest)
    ' Display the MIME types that can be used in the response.
    Dim types As String() = request.AcceptTypes

    If types IsNot Nothing Then
        Console.WriteLine("Acceptable MIME types:")

        For Each s As String In types
            Console.WriteLine(s)
        Next
    End If

    ' Display the language preferences for the response.
    types = request.UserLanguages

    If types IsNot Nothing Then
        Console.WriteLine("Acceptable natural languages:")

        For Each l As String In types
            Console.WriteLine(l)
        Next
    End If

    ' Display the URL used by the client.
    Console.WriteLine("URL: {0}", request.Url.OriginalString)
    Console.WriteLine("Raw URL: {0}", request.RawUrl)
    Console.WriteLine("Query: {0}", request.QueryString)

    ' Display the referring URI.
    Console.WriteLine("Referred by: {0}", request.UrlReferrer)

    ' Display the HTTP method.
    Console.WriteLine("HTTP Method: {0}", request.HttpMethod)

    ' Display the host information specified by the client.
    Console.WriteLine("Host name: {0}", request.UserHostName)
    Console.WriteLine("Host address: {0}", request.UserHostAddress)
    Console.WriteLine("User agent: {0}", request.UserAgent)
End Sub

注解

属性 Url 允许您从 Uri 对象获取所有可用信息。 如果只需要知道 URI 请求的原始文本,请考虑改用 RawUrl 属性。

如果无法分析来自客户端的 ,UriUrl 属性为 null。

属性 UnescapeRequestUrl 指示是否 HttpListener 使用原始非转义 URI 而不是转换后的 URI,其中转换了任何百分比编码值并执行其他规范化步骤。

HttpListener 实例通过 http.sys 服务接收请求时,它会创建 http.sys 提供的 URI 字符串的实例,并将其公开为 HttpListenerRequest.Url 属性。

http.sys 服务公开两个请求 URI 字符串:

  • 原始 URI

  • 转换后的 URI

原始 URI 是 HTTP 请求的请求行中提供的 System.Uri

GET /path/

Host: www.contoso.com

http.sys 为上述请求提供的原始 URI 是“/path/”。 这表示 HTTP 谓词后面的字符串,因为它是通过网络发送的。

服务 http.sys 使用 HTTP 请求行中提供的 URI 和 Host 标头根据请求中提供的信息创建转换的 URI,以确定请求应转发到的源服务器。 这种判断是通过将请求中的信息与一组已注册的 URI 前缀进行比较做出的。 为了能够比较这些值,需要对请求进行一些规范化。 对于上面的示例,转换后的 URI 如下所示:

http://www.contoso.com/path/

http.sys 服务将 Uri.Host 属性值与请求行中的字符串相组合,以创建转换后的 URI。 此外, http.sysSystem.Uri 类还执行以下操作:

  • 取消转义所有百分比编码值。

  • 将百分比编码的非 ASCII 字符转换为 UTF-16 字符表示形式。 请注意,支持 UTF-8 和 ANSI/DBCS 字符以及 Unicode 字符(使用 %uXXXX 格式的 Unicode 编码)。

  • 执行其他规范化步骤,例如路径压缩。

由于请求不包含任何有关百分比编码值使用的编码的信息,因此仅仅是通过分析百分比编码值可能无法确定正确的编码。

因此 http.sys 提供了两个注册表项用于修改进程:

注册表项 默认值 说明
EnableNonUTF8 1 如果为零,则 http.sys 仅接受 UTF-8 编码的 URL。

如果不为零,则 http.sys 还接受请求中 ANSI 编码或 DBCS 编码的 URL。
FavorUTF8 1 如果不为零,则 http.sys 始终首先尝试将 URL 解码为 UTF-8;如果这种转换失败并且 EnableNonUTF8 不为零,则 Http.sys 随后会尝试将 URL 解码为 ANSI 或 DBCS。

如果为零(并且 EnableNonUTF8 不为零),则 http.sys 会尝试将 URL 解码为 ANSI 或 DBCS;如果不成功,则尝试进行 UTF-8 转换。

HttpListener 收到请求时,它将使用 http.sys 中转换的 URI 作为 Url 属性的输入。

除了 URI 中的字符和数字外,还需要指定支持字符。 例如,以下 URI 用于检索客户编号“1/3812”的客户信息:

http://www.contoso.com/Customer('1%2F3812')/

请注意 Uri (%2F) 中的百分比编码斜杠。 这种编码是必要的,因为在本例中,斜杠字符代表数据而不是路径分隔符。

将字符串传递给 Uri 构造函数会生成以下 URI:

http://www.contoso.com/Customer('1/3812')/

将路径拆分为多个段会生成以下元素:

Customer('1

3812')

这不是请求发送者的意图。

如果 属性 UnescapeRequestUrl 设置为 false,则当 收到请求时 HttpListener ,它将使用原始 URI 而不是 从 http.sys 转换的 URI 作为属性的 Url 输入。

适用于

另请参阅