通过 C# 将消息放入 Websphere MQ 的数据长度与手动放入相同消息的数据长度不同
MQMessage queueMessage = new MQMessage();
queueMessage.WriteString(strInputMsg);
queueMessage.Format = MQC.MQFMT_STRING;
MQPutMessageOptions queuePutMessageOptions = new MQPutMessageOptions();
Queue.Put(queueMessage, queuePutMessageOptions);
使用C#,使用上面的代码,当我将消息输入到队列中时,消息的数据长度为3600。
当我通过右键单击队列并选择“放置测试消息”选项手动将消息输入到队列中时,数据长度这条消息是1799。
我真的很困惑为什么会这样。两种情况下的消息都是带有声明的 xml 字符串。在Notepad++中,包括声明在内共有1811个字符。当我在输入队列之前在调试器中查看消息时,消息将转换为 xml,没有任何行或回车。
我使用以下方法创建了 xml 字符串:
//converts string message into xml by serializing it
public string GetMessage(MyMessage messageInstance)
{
// Serialize the request
XmlSerializer xsr = new XmlSerializer(typeof(MyMessage));
MemoryStream memoryStream = new MemoryStream();
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xsr.Serialize(xmlTextWriter, messageInstance);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
string XmlizedString = new UTF8Encoding().GetString((memoryStream.ToArray());
// Encode the xml
Encoding utf = Encoding.UTF8;
byte[] utfBytes = utf.GetBytes(XmlizedString);
// Load the document (XmlResolver is set to null to ingore DTD)
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.XmlResolver = null;
xmlDoc.LoadXml(utf.GetString(utfBytes));
return utf.GetString(utfBytes);
我在 C# 实现中是否遗漏了添加额外字符的任何内容?
谢谢。
MQMessage queueMessage = new MQMessage();
queueMessage.WriteString(strInputMsg);
queueMessage.Format = MQC.MQFMT_STRING;
MQPutMessageOptions queuePutMessageOptions = new MQPutMessageOptions();
Queue.Put(queueMessage, queuePutMessageOptions);
Using C#, with the above code, when I input the message into the queue, the data length of the message is 3600.
When I manually input the message into the queue by right clicking the queue and selecting Put Test Message option, the data length of the message is 1799.
I am really confused why this is the case. The message in both cases is an xml string with declaration. In Notepad++, there are 1811 characters including the declaration. When I view the message in debugger before I input into the queue, the message is converted into xml without any line or return carriages.
I created the xml string using:
//converts string message into xml by serializing it
public string GetMessage(MyMessage messageInstance)
{
// Serialize the request
XmlSerializer xsr = new XmlSerializer(typeof(MyMessage));
MemoryStream memoryStream = new MemoryStream();
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
xsr.Serialize(xmlTextWriter, messageInstance);
memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
string XmlizedString = new UTF8Encoding().GetString((memoryStream.ToArray());
// Encode the xml
Encoding utf = Encoding.UTF8;
byte[] utfBytes = utf.GetBytes(XmlizedString);
// Load the document (XmlResolver is set to null to ingore DTD)
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.XmlResolver = null;
xmlDoc.LoadXml(utf.GetString(utfBytes));
return utf.GetString(utfBytes);
Am I missing anything in my C# implementation that is adding extra characters?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如@Matten 所说,一个问题可能是字符编码。
CharacterSet 属性为 1200 (UNICODE) 且 WriteString< /a> 转换为CharacterSet 指定的代码页。
代码页 1200 是 UTF-16 小尾数法,因此每个字符可能会获得两个字节。 “Put Test Message”当然有可能使用某种其他编码,对常见字符使用每个字符一个字节。
假设3600和1799长度以字节为单位计算,它们可以表示1800个UTF-16LE字符和1799个UTF-8字符(或1799个ASCII字符或1799个EBCDIC字符...)。
这仍然使我们在长度上存在一个字符的差异。也许 WriteString 在写入的字符串中包含终止 NULL 字符?
您确定相信 Notepad++ 给您的计数吗?如果“放置测试消息”将 1799 个字符放入消息中,则您提供给消息的数据中可能有 1799 个字符。
编辑:假设编码理论是正确的,您可以使用不同的编码来缩短消息。编码使特定消息变得多短取决于字符串的实际内容。
例如,您可以使用 ASCII 编码来获取每个字符一个字节。
如果 xml 字符串中的所有字符都采用 ASCII 表示,那么会将消息缩短至 1800 个字节。
另一种方法是使用 UTF-8 编码。
使用 UTF-8 的优点是(与 ASCII 不同)所有字符都有表示形式(对于“all”的某些值)。缺点是有些字符需要两个、三个甚至四个字节来表示。最常见的字符用一个字节编码,然后次最常见的字符用两个字节编码,依此类推。
在最好的情况下,UTF-8 编码也将为您提供 1800 字节。在最坏的情况下,它会给你 7200 字节,但这似乎不太可能,除非你使用像克林贡语这样的东西!
As @Matten suggests one problem could be character encoding.
The default value for the CharacterSet property is 1200 (UNICODE) and WriteString converts to the code page specified by CharacterSet.
Code Page 1200 is UTF-16 little-endian so you are likely to get two bytes per character. It is certainly possible that "Put Test Message" uses some other encoding that uses one-byte per character for common characters.
Assuming that the 3600 and 1799 lengths are counted in bytes, they could represent 1800 UTF-16LE characters and 1799 UTF-8 characters (or 1799 ASCII characters or 1799 EBCDIC characters...).
That still leaves us with a one character difference in length. Perhaps WriteString includes a terminating NULL character in the string written?
Are you sure you trust the count Notepad++ gives you? If Put Test Message placed 1799 characters into a message there were probably 1799 characters in the data you supplied to it.
Edit: Assuming the encoding theory is correct, you could shorten the message by using a different encoding. How short an encoding would make a particular message would depend on the actual contents of the string.
For example, you could use an ASCII encoding to get one byte per character.
That would shorten your message to 1800 bytes if all the characters in your xml string had an ASCII representation.
An alternative would be to use UTF-8 encoding.
Using UTF-8 has the advantage that (unlike ASCII) all characters have a representation (for certain values of 'all'). The disadvantage is that some characters require two, three or even four bytes to represent them. The most common characters are encoded in one byte, then the next most common characters are encoded in two bytes and so on.
In the best case a UTF-8 encoding would also give you 1800 bytes. In the worst case it would give you 7200 bytes but that seems very unlikely unless you are using something like Klingon!