1

I'm trying to post XML from www.domain1.com a controller in www.domain2.com.

Here is my code:

www.domain2.com

[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");
        }

Then in www.domain1.com I am posting as follows..

private string getProductLIstXML()
    {
        ASCIIEncoding encoding = new ASCIIEncoding();

        string SampleXml = "<testXml>test</testXml>";

        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();
        }
    }

My Custom Route in RouteProvider class

   routes.MapLocalizedRoute("FetchProductsXML",
                            "products-xml",
                            new { controller = "Product", action = "FetchProductsXML" },
                            new[] { "Nop.Web.Controllers" });

The error I get is

System.Net.WebException: The remote server returned an error: (404) Not Found.
   at System.Net.HttpWebRequest.GetResponse()

I've followed what the examples from the following SOQ's

How to POST XML into MVC Controller? (instead of key/value)

HTTP post XML data in C#

Orion
  • 452
  • 6
  • 23
  • Can you post the code for the products-xml endpoint? That's the part it can't find. – Vic F Jun 26 '18 at 01:26
  • @VicF products-xml is for the FetchProductsXML action. I have this setup in my RouteConfig file. I'm not sure about setting up an endpoint? I thought this is all I needed to do? – Orion Jun 26 '18 at 01:33
  • Try changing `FetchProductsXML` into an `HttpGet` and see if you can access the api url. It's possible you have incorrect routing. – jegtugado Jun 26 '18 at 02:59
  • @JohnEphraimTugado I tried that, still nothing doing :( – Orion Jun 26 '18 at 03:09
  • I've updated my question to include the custom route – Orion Jun 26 '18 at 03:10

1 Answers1

0

After a long night, I realized I had the method in the incorrect class :(

All fixed now.

Orion
  • 452
  • 6
  • 23