我有一个程序,我使用 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>')]