如何从 .NET 客户端向远程 ColdFusion 服务器发送 XML 数据?

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

我正在尝试将表单序列化为 XML 并将其发送到 ColdFusion 服务器 (CF 8)。简单吧?好吧,我不断收到一条错误消息,说 "ByteArray objects cannot be converted to strings." 我绝对不是 CF 专家,所以我不确定在 .NET 方面该怎么做才能解决这个问题。 ColdFusion 服务器属于第三方公司,我无权访问 CF Admin。下面是我用来向 ColdFusion 服务器发送 POST 请求的 C# 代码:

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

如果我能提供更多信息,请告诉我。任何帮助是极大的赞赏。谢谢。

更新:输出

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

我不确定我能否解释原因,但问题是请求 header 中的 Content-Type 被设置为 application/xml 而它应该是 text/xml.我希望这会在将来帮助某人。如果您遇到类似问题,请务必使用 Fiddler 或其他一些网络调试软件来检查您的 HTTP 请求,以确保它们看起来正确。