แก้ไข

แชร์ผ่าน


HttpRequestMessageProperty.Headers Property

Definition

Gets the HTTP headers from the HTTP request.

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

Property Value

Returns a WebHeaderCollection that contains the HTTP headers in the HTTP request.

Examples

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

    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

Remarks

There are situations in which this property is not available and attempts to access it may cause an exception. 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.

Applies to