共用方式為


HttpRequestMessageProperty.Headers 屬性

定義

從 HTTP 要求取得 HTTP 標頭。

public:
 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 標頭。

範例

下列程式碼會示範如何使用這個屬性,將自訂標頭加入至訊息中。

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");

    try
    {
        messageToSend = Message.CreateMessage(MessageVersion.Soap11, "http://tempuri.org/IUntypedService/ProcessMessage", "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")

    Try
        messageToSend = Message.CreateMessage(MessageVersion.Soap11, "http://tempuri.org/IUntypedService/ProcessMessage", "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

備註

某些狀況下,無法使用這個屬性,而且嘗試存取它可能會造成例外狀況。 當WCF裝載於IIS時,當 HTTP要求完成之後,就會發生這種情況,特別是在訊息可用之前傳回回復的單向案例中。

適用於