title: POST request to the WCF service by the use of HttpWebRequest
body: <p>My Service Contract</p>
  
  <pre><code>[ServiceContract]
  public interface ITsdxService
  {
      [OperationContract]
      [WebGet(UriTemplate="/GetTestCostCentre")]
      CostCentre GetTestCostCentre();
  
      [OperationContract]
      [WebInvoke(UriTemplate="/SetCostCentre", Method="POST")]
      string SetCostCentre(CostCentre cc);
  }
  
  public class TsdxService : ITsdxService
  {
      public CostCentre GetTestCostCentre()
      {
          CostCentre cc = new CostCentre();
          cc.Code = "Test";
          cc.Name = "Test Cost Centre";
          cc.Description = new byte[] { 12, 34, 89, 240, 66, 87, 189 };
          cc.SAPStatus = "Existent";
          cc.SAPSiteFolder = "Folder1";
          return cc;
      }
  
      public string SetCostCentre(CostCentre cc)
      {
          return cc.Code;
      }
  }
  </code></pre>
  
  <p>Then I start this service and try to work with it from different application:</p>
  
  <pre><code>Uri requestUri = new Uri(textBox1.Text + "/tsdx/GetTestCostCentre");
  
  HttpWebRequest request = WebRequest.Create(requestUri) as HttpWebRequest;
  XElement root;
  
  using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
  {
      StreamReader reader = new StreamReader(response.GetResponseStream());
      root = XElement.Parse(reader.ReadToEnd());
      textBox2.Text = root.ToString();
  }
  </code></pre>
  
  <p>All is ok, I am getting the xml document. But I've faced with problem when I tried to send POST request to this service:</p>
  
  <pre><code>Uri requestUri = new Uri(textBox1.Text + "/tsdx/SetCostCentre");
  
  HttpWebRequest request = WebRequest.Create(requestUri) as HttpWebRequest;
  
  byte[] bytes = Encoding.UTF8.GetBytes(textBox2.Text);
  
  request.ContentLength = bytes.Length;
  request.Method = "POST";
  
  Stream dataStream = request.GetRequestStream();
  dataStream.Write(bytes, 0, bytes.Length);
  dataStream.Close();
  
  using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
  {
      StreamReader reader = new StreamReader(response.GetResponseStream());
      textBox2.Text = reader.ReadToEnd();
  }
  </code></pre>
  
  <p>Exception: The remote server returned an error: (400) Bad Request.</p>
  
  <p>What I did wrong?</p>