title: Trying to call a WCF service with a WebRequest
body: <p>I have a WCF service that needs to be called by a 3rd party app, posting some raw XML.</p>
  
  <p>I am trying to test my service by constructing a simple WebRequest and making the request to the service.</p>
  
  <p>Here's my service code:</p>
  
  <p>Interface:</p>
  
  <pre><code>    [ServiceContract(Namespace = "http://test.mydomain.com")]
  public interface ITest
  {
      [WebInvoke(UriTemplate = "", BodyStyle = WebMessageBodyStyle.Bare, Method="POST")]
      [OperationContract]
      Stream SaveXML(Stream input);
  }
  </code></pre>
  
  <p>Service:</p>
  
  <pre><code>    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  [ServiceBehavior(Namespace = "http://test.mydomain.com")]
  public class Test : ITest
  {
      public Stream SaveXML(Stream input)
      {
          StreamReader streamReader = new StreamReader(input);
          string rawString = streamReader.ReadToEnd();
          streamReader.Dispose();
  
          // here need to save the input stream to xml format file
          Encoding encoding = Encoding.GetEncoding("ISO-8859-1");
          WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
          byte[] returnBytes = encoding.GetBytes(rawString);
          return new MemoryStream(returnBytes);
      }
  }
  </code></pre>
  
  <p>config:</p>
  
  <pre><code>    &lt;services&gt;
    &lt;service behaviorConfiguration="Blah.TestBehavior" name="Blah.Test"&gt;
      &lt;endpoint address="http://localhost:51494/Blah/Test.svc" binding="basicHttpBinding" contract="Blah.ITest"&gt;
        &lt;identity&gt;
          &lt;dns value="localhost" /&gt;
        &lt;/identity&gt;
      &lt;/endpoint&gt;
      &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /&gt;
    &lt;/service&gt;
  &lt;/services&gt;
  </code></pre>
  
  <p>faulty client code:</p>
  
  <pre><code>            string postData = "&lt;Message version=\"1.5\" xmlns=\"http://test.mydomain.com\" &gt;&lt;books&gt;Blah&lt;/books&gt;&lt;/Message&gt;";
          WebRequest request = WebRequest.Create("http://localhost:51494/Blah/Test.svc/SaveXML");
          request.Method = "POST";
  
          byte[] byteArray = Encoding.UTF8.GetBytes(postData);
          request.ContentType = "application/x-www-form-urlencoded";
          //request.ContentType = "text/xml; charset=utf-8";
          //request.ContentType = "text/xml;";
          //request.ContentType = "application/xml;";
          request.ContentLength = byteArray.Length;
  
          Stream dataStream = request.GetRequestStream();
          dataStream.Write(byteArray, 0, byteArray.Length);
          dataStream.Close();
  
          // Get the response.
          WebResponse response = request.GetResponse();
  </code></pre>
  
  <p>On that last line I get a 400 (Bad Request) or 415 (Unsupported Media Type) error, depending on which ContentType I specify.</p>
  
  <p>Also, if I add a service reference in my client app, and call the service using the API it works fine. Any insights would be greatly appreciated, as I am new to WCF and completely stumped.</p>