0

我有一个程序,我使用 POST 将 XML 消息发送到端点。它在转换为字节数组之前和之后的输出中都正确形成,但是一旦端点接收到它,第一个 = 就会变成 a ,从而导致解析问题。

<?xml version="1.0" encoding="utf-8"?>变为<?xml version', '"1.0" encoding="utf-8"?>,但仅适用于最外层标签。

谁能澄清为什么会发生这种情况,以及如何防止它?我的任何调整似乎都没有帮助。

谢谢!

(注意:我尝试使用 XML 序列化类,但它的输出量很大,有时每 3 秒高达 9-12 条消息,并且由于它为每条消息生成 csc.exe,因此导致内存管理问题。)

这是我的代码:

        DateTimeOffset dateOffset = new DateTimeOffset(DateTime.Now,
                        TimeZoneInfo.Local.GetUtcOffset(DateTime.Now));


        string xmlToSend = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
            + "<message type=\"" + "VEHICLE" + "\" time=\"" + dateOffset.ToString("o") + "\" "
            + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"
            + "<tag1>" + "TESTER" + "</tag1>"
            + "<tag2>" + "TESTER" + "</tag2>"
            + "<tag3>" + "TESTER" + "</tag3>"
            + "<tag4>" + "TESTER" + "</tag4>"
            + "<confidence>" + "TESTER" + "</confidence>"
            + "</message>";

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xmlToSend);

        Console.WriteLine("doc to string: " + doc.OuterXml);

        byte[] bytes;
        bytes = System.Text.Encoding.ASCII.GetBytes(doc.OuterXml);
        Console.WriteLine("bytes to string: " + Encoding.Default.GetString(bytes));


            Uri temp = new Uri("http://localhost:1337/");


            ThreadPool.QueueUserWorkItem((WaitCallback)delegate(Object myObj)
            {
                try
                {

                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(temp);

                    //Set HttpWebRequest properties

                    request.Method = "POST";
                    request.ContentType = "text/xml; encoding='utf-8'";
                    request.ContentLength = bytes.Length;
                    request.Timeout = 5000;

                    Stream requestStream = request.GetRequestStream();
                    requestStream.Write(bytes, 0, bytes.Length);


                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Console.WriteLine("Endpoint: " + temp.AbsoluteUri + "; Status code: " + response.StatusCode);



                    response.Close();
                    requestStream.Close();
                    request = null;

                }


                catch (Exception e)
                {
                        Console.WriteLine("ERROR: Posting to the endpoint didn't work - " + e.Message + "(" + temp.AbsoluteUri + ")");

                }

            });

这是控制台输出:

doc to string: <?xml version="1.0" encoding="utf-8"?><message type="VEHICLE" tim
e="2013-05-17T15:44:38.3593750-07:00" xmlns:xsi="http://www.w3.org/2001/XMLSchem
a-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><tag1>TESTER</tag1><tag
2>TESTER</tag2><tag3>TESTER</tag3><tag4>TESTER</tag4><confidence>TESTER</confide
nce></message>
bytes to string: <?xml version="1.0" encoding="utf-8"?><message type="VEHICLE" t
ime="2013-05-17T15:44:38.3593750-07:00" xmlns:xsi="http://www.w3.org/2001/XMLSch
ema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><tag1>TESTER</tag1><t
ag2>TESTER</tag2><tag3>TESTER</tag3><tag4>TESTER</tag4><confidence>TESTER</confi
dence></message>
Endpoint: http://localhost:1337/; Status code: OK

和我的本地服务器输出:

[('<?xml version', '"1.0" encoding="utf-8"?><message type="VEHICLE" time="2013-0
5-17T15:44:38.3593750-07:00" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instanc
e" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><tag1>TESTER</tag1><tag2>TESTER<
/tag2><tag3>TESTER</tag3><tag4>TESTER</tag4><confidence>TESTER</confidence></mes
sage>')]
4

2 回答 2

0

这是一个编码问题,您的端点尽最大努力理解包含错误的内容

    bytes = System.Text.Encoding.ASCII.GetBytes(doc.OuterXml);
    Console.WriteLine("bytes to string: " + Encoding.Default.GetString(bytes));

您以 ASCII 编码创建字节,然后使用默认编码(即 ANSI)进行输出。然后,在 XML 和请求内容类型中,您告诉服务器这将是 UTF-8:

    request.ContentType = "text/xml; encoding='utf-8'";

如果可能,您应该始终使用 UTF-8。所以摆脱 ASCII 和 ANSI(默认),用 UTF-8 编码,你的问题可能会消失。

于 2013-05-17T23:11:00.623 回答
0

我想知道您的响应内容类型是否默认为错误的值并让客户感到困惑 - 设置它可能会有所帮助:

response.ContentType = "application/xml";
于 2013-05-17T23:12:54.910 回答