我有一个程序,其中我使用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>')]
这是一个编码问题,您的端点正在尽最大努力理解包含错误的内容
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编码,您的问题可能会消失。
我想知道您的响应内容类型是否默认为错误的值并使客户端感到困惑-设置它可能会有所帮助:
response.ContentType = "application/xml";