title: c# WCF HTTP rest client with WebHttpBinding/Behavior for POSTing RAW data in request body
body: <p>I have a HTTP REST service endpoint I want to connect to send an XML stream in RAW format. Therefore I created a client side service contract "IHttpXmlClient" with a respective ClientBase inherited implementation.</p>
  
  <pre><code>using System.ServiceModel.Web;
  using System.ServiceModel;
  using System.IO;
  
  namespace MyNamespace
  {
    [ServiceContract]
    public interface IHttpXmlClient
    {
      [OperationContract]
      [WebInvoke(BodyStyle = WebMessageBodyStyle.Bare, Method = "POST")]
      void Send(Stream data);
    }
  
    public class HttpXmlClient : ClientBase&lt;IHttpXmlClient&gt;, IHttpXmlClient
    {
      public void Send(Stream data)
      {
        Channel.Send(data);
      }
    }
  }
  </code></pre>
  
  <p>As configuration I defined the following in my App.config:</p>
  
  <pre><code>&lt;system.serviceModel&gt;
    &lt;client&gt;
      &lt;endpoint address="http://thehttpxmlserver.com:8000/receiver?client=itsme"
            binding="webHttpBinding"
            bindingConfiguration="webHttp_HttpXmlClient"
            contract="MyNamespace.IHttpXmlClient"
            behaviorConfiguration="HttpXmlClientEndpointBehavior"&gt;
      &lt;/endpoint&gt;
    &lt;/client&gt;
    &lt;bindings&gt;
      &lt;webHttpBinding&gt;
        &lt;binding name="webHttp_HttpXmlClient"&gt;
          &lt;security mode="TransportCredentialOnly"&gt;
            &lt;transport clientCredentialType="Basic" /&gt;
          &lt;/security&gt;
        &lt;/binding&gt;
      &lt;/webHttpBinding&gt;
    &lt;/bindings&gt;
    &lt;behaviors&gt;
      &lt;endpointBehaviors&gt;
        &lt;behavior name="HttpXmlClientEndpointBehavior"&gt;
          &lt;webHttp /&gt;       
        &lt;/behavior&gt;
      &lt;/endpointBehaviors&gt;
    &lt;/behaviors&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>The implementation to connect the HTTP REST service endpoint and send the respective XML data looks like this:</p>
  
  <pre><code>var myXml = "&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;&lt;root&gt;Hello World&lt;/root&gt;";
  using (var data = new MemoryStream(Encoding.UTF8.GetBytes(myXml)))
  {
    var client = new HttpXmlClient();
    client.ClientCredentials.UserName.UserName = "myuser";
    client.ClientCredentials.UserName.Password = "mypassword";
    client.Open();
    client.Send(data);
  }
  </code></pre>
  
  <p>Everything is just working perfectly except the "content-type" HTTP header contains the wrong information. I used fiddler to trace the raw outgoing information from my machine. Here goes the result:</p>
  
  <pre><code>POST http://thehttpxmlserver.com:8000/receiver?client=itsme HTTP/1.1
  Content-Type: application/octet-stream
  Authorization: Basic bXl1c2VyOm15cGFzc3dvcmQ=
  Host: thehttpxmlserver.com:8000
  Content-Length: 65
  Expect: 100-continue
  
  &lt;?xml version="1.0" encoding="utf-8"?&gt;&lt;root&gt;Hello World&lt;/root&gt;
  </code></pre>
  
  <p>What I want is to set the "content-type" header to the value "text/xml;charset=UTF-8". Is that possible somehow? Of course I want to keep my above implementation and I definitely don't want to implement a manual WebRequest, where I loose the easy configuration in my application's config file!</p>
  
  <p>Thank you very much in advance!</p>