Search code examples
c#.netxmlasp.net-mvccoldfusion-8

How could I send XML data to a remote ColdFusion server from a .NET client?


I am trying to serialize a form to XML and send it to a ColdFusion server (CF 8). Simple, right? Well, I keep getting an error message saying "ByteArray objects cannot be converted to strings." I am definitely not a CF expert, so I'm not sure what to do on the .NET side to fix this issue. The ColdFusion server belongs to a third party company and I have no access to the CF Admin whatsoever. Below is the C# code that I'm using to send the POST request to the ColdFusion server:

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


            try
            {
                var xmlSerializer = new XmlSerializer(typeof(Transmission));
                XmlWriterSettings xws = new XmlWriterSettings();
                XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", "");
                xws.OmitXmlDeclaration = true;
                using (StringWriter sw = new StringWriter())
                {
                    XmlWriter writer = XmlWriter.Create(sw, xws);
                    xmlSerializer.Serialize(writer, t, ns);
                    var contentData = sw.ToString();
                    var httpContent = new StringContent(contentData, Encoding.UTF8, "application/xml");
                    var httpClient = new HttpClient();
                    httpClient.Timeout = new TimeSpan(0, 1, 0);
                    //string httpContentAsString = await httpContent.ReadAsStringAsync();
                    var response = await httpClient.PostAsync("theUrlToCF", httpContent);
                    ViewBag.ResponseHtml = await response.Content.ReadAsStringAsync();
                    return View("Error");
                }
            }
            catch (Exception ex)
            {
                string message = ex.Message;
                ViewBag.ErrorMessage = ex.Message;
                return View("Error");
            }
        }

Please let me know if I can provide any further information. Any help is greatly appreciated. Thanks.

Update: Output

<Transmission>
   <TransmissionHeader>
      <UserId>1234</UserId>
      <Password>1234</Password>
   </TransmissionHeader>
   <MoveRequest>
      <BillToCode>example</BillToCode>
      <AuthorizationNumber>123456</AuthorizationNumber>
      <Caller>0</Caller>
      <Customer>C</Customer>
      <Cargo>
         <Shipper>
            <Name>example</Name>
            <AddressLine1>123 Sesame St</AddressLine1>
            <City>Tuscaloosa</City>
            <ZipCode>50231</ZipCode>
            <Country>United States</Country>
            <Phone>5555555</Phone>
            <Fax>555</Fax>
            <OpenTime>12:00</OpenTime>
            <CloseTime>17:00</CloseTime>
            <DefaultAirport>example</DefaultAirport>
            <DockHighRequired>false</DockHighRequired>
         </Shipper>
         <Consignee>
            <DockHighRequired>false</DockHighRequired>
         </Consignee>
         <CargoDetails>
            <NumberContainers>0</NumberContainers>
            <Pieces>0</Pieces>
            <WeightOf>0</WeightOf>
            <LengthOf>0</LengthOf>
            <WidthOf>0</WidthOf>
            <HeightOf>0</HeightOf>
            <LooseLoad>false</LooseLoad>
            <Stackable>false</Stackable>
            <Hazmat>false</Hazmat>
            <HazmatUNNumber>0</HazmatUNNumber>
            <CostPerPiece>0</CostPerPiece>
         </CargoDetails>
      </Cargo>
   </MoveRequest>
</Transmission>

Transmission Class:

[Serializable]
    public class Transmission
    {
        public TransmissionHeader TransmissionHeader { get; set; }
        public MoveRequest MoveRequest { get; set; }
    }

Solution

  • I'm not sure I can explain why, but the issue was that Content-Type in the request header was set to application/xml when it should have been text/xml. I hope this will help someone in the future. Be sure to use Fiddler or some other web debugging software to inspect your HTTP requests to make sure that they look correct if you're having similar issues.