Showing posts with label http header. Show all posts
Showing posts with label http header. Show all posts

Thursday, April 17, 2008

How to add custom http header in WCF

If you want to add custom http header web service calls, there is a simple way to do it in WCF. Here is the code:

MessageHeader header
= MessageHeader.CreateHeader(
"My-CustomHeader",
"http://myurl",
"Custom Header."
);

using (Service1Client client = new Service1Client())
{
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
OperationContext.Current.OutgoingMessageHeaders.Add(header);

HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers.Add("myCustomHeader", "Custom Happy Value.");
httpRequestProperty.Headers.Add(HttpRequestHeader.UserAgent, "my user agent");
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;

}
}