title: WCF Content-Length HTTP header on outbound message
body: <p>I'm in a tough situation in which a Java web service endpoint hosted on an IBM HTTP Server (IHS) requires a Content-Length header, although it supposedly conforms to HTTP/1.1. If I send the header, everything works. If I leave it off, I get a 500 error response informing me that my POST entity body was empty (even though it was not).</p>
  
  <p>We've invested significant time into our WCF client for these services (developed by a third party) and I can't seem to find a good way to append a Content-Length header to the request. I am able to add arbitrary headers (i.e. X-Dan-Lynn-Header) to the request using an IClientMessageInspector as described in <a href="http://msmvps.com/blogs/paulomorgado/archive/2007/04/27/wcf-building-an-http-user-agent-message-inspector.aspx" rel="nofollow noreferrer">blog posts like this</a>, but WCF seems to ignore a Content-Length header.</p>
  
  <p>My options are:</p>
  
  <p>a) figure out how to force WCF to append a Content-Length header to the HTTP POST request or,</p>
  
  <p>b) find or write an extremely simple-yet-transparent HTTP proxy that decorates the request with a Content-Length header.</p>
  
  <p>Thanks!</p>
  
  <h2>Sample IClientMessageInspector.BeforeSendRequest:</h2>
  
  <pre><code>
  public object BeforeSendRequest(ref Message request, IClientChannel channel)
  {
      var buffer = request.CreateBufferedCopy(Int32.MaxValue);
      var tempRequest = buffer.CreateMessage();
  
  
      HttpRequestMessageProperty httpRequest = GetHttpRequestProp(tempRequest);
      if (httpRequest != null)
      {
          if (string.IsNullOrEmpty(httpRequest.Headers[HttpRequestHeader.ContentLength]))
          {
              httpRequest.Headers.Add(HttpRequestHeader.ContentLength, GetMessageLength(buffer).ToString());
              httpRequest.Headers.Add("X-Dan-Lynn-Header", "abcdefghijk");
          }
  
      }
  
      request = tempRequest;
      request.Properties[HttpRequestMessageProperty.Name] = httpRequest;
  
      return null;
  }
  
  </code></pre>
  
  <h2>Sample request generated by WCF (and the preceding IClientMessageInspector):</h2>
  
  <pre><code>POST /path/to/service HTTP/1.1
  Content-Type: text/xml; charset=utf-8
  X-Dan-Lynn-Header: abcdefghijk
  SOAPAction: "http://tempuri.org/path/to/service/action"
  Host: service.host.tld
  Transfer-Encoding: chunked
  Connection: Keep-Alive
  
  
  &lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt;
      &lt;s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
          .......body removed for clarity......
  
      &lt;/s:Body&gt;
  &lt;/s:Envelope&gt;
  </code></pre>