0

I am attempting to send XML data to a remote ColdFusion server using the code below:

[HttpPost]
        public ActionResult Index(Transmission t)
        {
            ViewBag.ErrorMessage = "";
            ViewBag.OtherMessage = "";
            ViewBag.ResponseHtml = "";

            //set request properties
            var request = (HttpWebRequest)WebRequest.Create("theUrl");
            request.ContentType = "application/xml";
            request.ProtocolVersion = HttpVersion.Version10;
            request.Method = "POST";
            request.Timeout = 60000;

            //serialize the ViewModel
            XmlSerializer ser = new XmlSerializer(typeof(Transmission));
            XmlWriterSettings xws = new XmlWriterSettings();
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", "");
            xws.OmitXmlDeclaration = true; //omit the xml declaration line  
            Stream stream = new MemoryStream();
            StringBuilder sb = new StringBuilder();
            XmlWriter writer = XmlWriter.Create(sb, xws);
            ser.Serialize(writer, t, ns);

            using (StreamReader sr = new StreamReader(stream))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    sb.AppendLine(line);
                }
                byte[] postBytes = Encoding.UTF8.GetBytes(sb.ToString());
                request.ContentLength = postBytes.Length;

                try
                {
                    using (Stream requestStream = request.GetRequestStream())
                    {
                        requestStream.Write(postBytes, 0, postBytes.Length);
                        requestStream.Close();
                    }
                    using (var response = (HttpWebResponse)request.GetResponse())
                    {
                        ViewBag.ResponseHtml = response.ToString();
                        return View("Error"); //TODO: change this to success when I get the 500 error fixed
                    }

                }
                catch (Exception ex)
                {
                    ViewBag.ErrorMessage = ex.Message;
                    request.Abort();
                    return View("Error");
                }
            }    
        }

I have no access to the ColdFusion administrator, as it is a third-party vendor. According to a developer on their end, they are able to see transactions in their database logs, but I am getting a 500 error on my end. Is there something in the code I've posted that I should change, or would this be due to some setting on their side? The only information that I'm able to get is that it is a 500: Internal Server Error. When I put breakpoints in my code, the program throws an exception at this line: using (var response = (HttpWebResponse)request.GetResponse()).

Any help would be greatly appreciated. I would be happy to provide any further information necessary.

ic3man7019
  • 721
  • 6
  • 24
  • I would have thought that the 500 error returned would have some sort of stack trace or a slightly meaningful error message. – Steve Jan 30 '18 at 16:52
  • HttpVersion.Version11 is probably more appropriate. The 500 error is coming from them so they are the best people to ask. Run the request with Fiddler running so you have an example to send them and can also easily view any error details in the response body. – Alex K. Jan 30 '18 at 16:52
  • Sorry if it's me being blind, but where do you populate the Stream object that you're sending in the request? Are you sure you're sending them any request body? – Trevor Jan 30 '18 at 16:54
  • @AlexK. Thank you for the advice. Would you mind to tell me how to run it with Fiddler? – ic3man7019 Jan 30 '18 at 16:56
  • https://www.telerik.com/fiddler / https://stackoverflow.com/questions/1470634/get-http-requests-and-responses-made-using-httpwebrequest-httpwebresponse-to-sho – Alex K. Jan 30 '18 at 16:58
  • The code does not look correct. First on a request why are you setting the length : request.ContentLength = postBytes.Length;? Then why are you writing to the requestStream. When send messages to a server you usually create a response and write to the response object. – jdweng Jan 30 '18 at 17:13
  • When using fiddler with web connections look only at http messages. Each message from the client to the server should have a response from server to client. You should check the status of each response to see if you get a 200 done or something else which is normally an error. Also check the headers to make sure all the settings look correct. – jdweng Jan 30 '18 at 17:18

1 Answers1

1

While the 500 error may be coming from the front end server, Coldfusion has a setting in CF Admin under 'settings' called 'Enable HTTP Status Codes.' When enabled, it will send a 500 along with a CF error to whatever the front end server is, and that server may be configured to obfuscate the details and not relay the CF content.

So while it is possible that it's not from CF, it is a common configuration for production services not to be sending CF stack traces. Their logs should still show the actual error, or you could ask them to disable this setting temporarily.

Aquitaine
  • 96
  • 5