HttpRequestMessageProperty.Headers 屬性


從 HTTP 要求取得 HTTP 標頭。Gets the HTTP headers from the HTTP request.

 property System::Net::WebHeaderCollection ^ Headers { System::Net::WebHeaderCollection ^ get(); };
public System.Net.WebHeaderCollection Headers { get; }
member this.Headers : System.Net.WebHeaderCollection
Public ReadOnly Property Headers As WebHeaderCollection


傳回 WebHeaderCollection,其中包含 HTTP 要求內的 HTTP 標頭。Returns a WebHeaderCollection that contains the HTTP headers in the HTTP request.


下列程式碼會示範如何使用這個屬性,將自訂標頭加入至訊息中。The following code shows how to use this property to add custom headers to a message.

static Message BuildMessage()
    Message messageToSend = null;
    HttpRequestMessageProperty reqProps = new HttpRequestMessageProperty();
    reqProps.SuppressEntityBody = false;
    reqProps.Headers.Add("CustomHeader", "Test Value");
    reqProps.Headers.Add(HttpRequestHeader.UserAgent, "my user agent");

        messageToSend = Message.CreateMessage(MessageVersion.Soap11, "", "Hello WCF");
    catch (Exception e)
        Console.WriteLine("got exception when sending message: " + e.ToString());

    messageToSend.Properties[HttpRequestMessageProperty.Name] = reqProps;
    return messageToSend;
Private Shared Function BuildMessage() As Message
    Dim messageToSend As Message = Nothing
    Dim reqProps As New HttpRequestMessageProperty()
    reqProps.SuppressEntityBody = False
    reqProps.Headers.Add("CustomHeader", "Test Value")
    reqProps.Headers.Add(HttpRequestHeader.UserAgent, "my user agent")

        messageToSend = Message.CreateMessage(MessageVersion.Soap11, "", "Hello WCF")
    Catch e As Exception
        Console.WriteLine("got exception when sending message: " & e.ToString())
    End Try

    messageToSend.Properties(HttpRequestMessageProperty.Name) = reqProps
    Return messageToSend
End Function


某些狀況下,無法使用這個屬性,而且嘗試存取它可能會造成例外狀況。There are situations in which this property is not available and attempts to access it may cause an exception. 當 WCF 裝載于 IIS 時,在 HTTP 要求完成之後,特別是在單向案例中,在訊息變成可供使用之前,會傳回回複,這可能會發生這種情況。This can happen when WCF is hosted in IIS, after the HTTP request is complete, especially in one-way scenarios where the reply is sent back before the message has become available.