Share via


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 屬性。

如果Uri無法剖析來自用戶端的 ,則Url屬性為 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.sys 類別 System.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 會接著嘗試將其解碼為 ANSI 或 DBCS。

如果為零 (且 EnableNonUTF8 為非零),http.sys 會嘗試將其解碼為 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,而不是將轉換的 URI 從 http.sys 做為屬性的Url輸入。

適用於

另請參閱