Share via


HttpListenerRequest.UrlReferrer 屬性

定義

取得用戶端用來參考伺服器之資源的統一資源識別元 (URI)。

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

屬性值

Uri

Uri 物件,包含要求之 Referer 標頭的文字,如果要求中不包含此標頭,則為 null

範例

下列程式代碼範例示範如何使用這個屬性。

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

備註

如果用戶端已遵循所要求 URI 的超連結,其要求可能包含 Referrer 標頭,識別包含超連結之資源的 URI。

用戶端可以假化或選擇不呈現 Referer 標頭。 因此,雖然 UrlReferrer 屬性可用於識別 Web 流量中的基本趨勢,但您不應該使用它作為授權配置的一部分,以控制數據的存取。

如需要求標頭的完整清單,請參閱 HttpRequestHeader 列舉。

UrlReferrer如果要求中沒有Referrer標頭,或標頭存在於要求中,但未剖析為有效的 UriReferrer,則 為 null。

適用於

另請參閱