title: Post XML to MVC Controller C#
body: <p>I'm trying to post XML from www.domain1.com a controller in www.domain2.com.</p>
  
  <p>Here is my code:</p>
  
  <p>www.domain2.com</p>
  
  <pre><code>[HttpPost]
          public ActionResult FetchProductsXML()
          {
              Response.ContentType = "text/xml";
  
              StreamReader reader = new StreamReader(Request.InputStream);
              String xmlData = reader.ReadToEnd();
  
              var products = _productService.SearchProducts(showHidden: false);
              var xml = _exportManager.ExportProductsToXml(products);
  
              return this.Content(xml, "text/xml");
          }
  </code></pre>
  
  <p>Then in www.domain1.com I am posting as follows..</p>
  
  <pre><code>private string getProductLIstXML()
      {
          ASCIIEncoding encoding = new ASCIIEncoding();
  
          string SampleXml = "&lt;testXml&gt;test&lt;/testXml&gt;";
  
          try
          {
              byte[] data = encoding.GetBytes(SampleXml);
  
              string url = "http://www.domain2.com/products-xml";
  
              HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
              byte[] bytes;
              bytes = System.Text.Encoding.ASCII.GetBytes(SampleXml);
              request.ContentType = "text/xml; encoding='utf-8'";
              request.ContentLength = bytes.Length;
              request.Method = "POST";
              Stream requestStream = request.GetRequestStream();
              requestStream.Write(bytes, 0, bytes.Length);
              requestStream.Close();
              HttpWebResponse response;
              response = (HttpWebResponse)request.GetResponse();
              if (response.StatusCode == HttpStatusCode.OK)
              {
                  Stream responseStream = response.GetResponseStream();
                  string responseStr = new StreamReader(responseStream).ReadToEnd();
                  return responseStr;
              }
              return null;
  
          }
          catch (WebException webex)
          {
              return webex.ToString();
          }
      }
  </code></pre>
  
  <p>My Custom Route in RouteProvider class</p>
  
  <pre><code>   routes.MapLocalizedRoute("FetchProductsXML",
                              "products-xml",
                              new { controller = "Product", action = "FetchProductsXML" },
                              new[] { "Nop.Web.Controllers" });
  </code></pre>
  
  <p>The error I get is</p>
  
  <pre><code>System.Net.WebException: The remote server returned an error: (404) Not Found.
     at System.Net.HttpWebRequest.GetResponse()
  </code></pre>
  
  <p>I've followed what the examples from the following SOQ's</p>
  
  <p><a href="https://stackoverflow.com/questions/17951384/how-to-post-xml-into-mvc-controller-instead-of-key-value#">How to POST XML into MVC Controller? (instead of key/value)</a></p>
  
  <p><a href="https://stackoverflow.com/questions/17535872/http-post-xml-data-in-c-sharp">HTTP post XML data in C#</a></p>