通过C#中的WebSphere MQ把消息比手动把同样的信息不同的数据长度 [英] Putting Message in Websphere MQ via C# has different data length than manually putting the same message

查看:232
本文介绍了通过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。

Using C#, with the above code, when I input the message into the queue, the data length of the message is 3600.

当我手动输入信息到通过右键单击队列,然后选择把测试消息选项,队列,该消息的数据长度为1799。

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.

我真的很困惑,为什么是这种情况。这两种情况的消息是一个XML字符串声明。在记事本++中,有1811个字符,包括报关。当我认为我之前输入的调试器的消息到队列中,该消息被转换成XML没有任何行或返回车

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.

我创建使用XML字符串:

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

我缺少的是增加额外的角色我的C#实现什么?

Am I missing anything in my C# implementation that is adding extra characters?

谢谢。

推荐答案

由于@Matten提出一个问题可能是字符编码。

As @Matten suggests one problem could be character encoding.

对于的默认值CHARACTERSET 属性为1200(Unicode)的和的 WriteString 转换为字符集所指定的代码页。

The default value for the CharacterSet property is 1200 (UNICODE) and WriteString converts to the code page specified by CharacterSet.

代码页1200是UTF-16小端,所以你很可能得到每个字符两个字节。这当然是可能的,把测试消息用使用了常用的字符,每个字符一个字节的一些其他的编码。

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.

假设3600和1799的长度以字节为单位计算,他们可以代表1800 UTF-16LE字符和1799 UTF-8字符(或1799个ASCII字符或1799 EBCDIC字符...)。

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...).

这仍然给我们留下了长度为一个字符的区别。也许WriteString包括写入字符串中终止空字符?

That still leaves us with a one character difference in length. Perhaps WriteString includes a terminating NULL character in the string written?

您确定您信任计数记事本++给你吗?如果把测试消息放置1799个字符到消息有你提供给它的数据大概1799字

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.

编辑:假设编码理论是正确的,你可以通过使用不同的编码缩短的消息。如何短的编码将使一个特定的消息将取决于字符串的实际内容。

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.

例如,你可以使用ASCII编码得到每个字符一个字节。

For example, you could use an ASCII encoding to get one byte per character.

MQMessage queueMessage = new MQMessage();
queueMessage.CharacterSet = 437;  // Set code page to ASCII

这会缩短你的消息,1800字节如果所有的在你的XML字符串中的字符有一个ASCII码表示。

That would shorten your message to 1800 bytes if all the characters in your xml string had an ASCII representation.

另一种方法是使用UTF-8编码。

An alternative would be to use UTF-8 encoding.

MQMessage queueMessage = new MQMessage();
queueMessage.CharacterSet = 1208;  // Set code page to UTF-8



使用UTF-8的优点是(不同于ASCII)所有字符的表示(为'所有'一定值)。其缺点是一些字符需要两个,三个或甚至四个字节来表示它们。最常见的字符在一个字节编码,那么下一个最常见的字符被编码在两个字节等等。

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.

在最好的情况下,UTF-8编码也给你1800个字节。在最坏的情况下,它会给你7200个字节,但似乎不太可能,除非你使用像克林贡语!

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!

这篇关于通过C#中的WebSphere MQ把消息比手动把同样的信息不同的数据长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆