.net, js, html, arduino, java... no rants or clickbaits.

Sending message to WebSphere MQ in the UTF-8

If you want to put a message encoded in UTF-8 to WebSphere MQ queue, be sure to set CharacterSet property of MQMessage object to 1208. If you do not, the text will be encoded using UTF-16 (CCSID 1200).

MQQueueManager queueManager = new MQQueueManager(...);
MQQueue queue = queueManager.AccessQueue(...);
MQPutMessageOptions putMessageOptions = new MQPutMessageOptions(...);
MQMessage message = new MQMessage();

message.Format = MQC.MQFMT_STRING;
message.CharacterSet = 1208;
queue.Put(message, putMessageOptions);

Strings in .NET are encoded with UTF-16. Sometimes however, it is worthwhile to use UTF-8 for information exchange. Why? Version 8 is less verbose because characters from the US-ASCII table are encoded using 1 byte instead of using 2 as in the case of UTF-16. So if you send the text consisting solely of that character set, you will use two times less space! In the case of Polish letters, characters will be encoded with 2 bytes (as for UTF-16).

Here is a comparison of bytes for the text "abcąćę":

UTF-8    61 62 63 C4 85 C4 87 C4 99
UTF-16   61 00 62 00 63 00 05 01 07 01 19 01

Session access from Page Methods

ASP.NET AJAX offers easy to use and effective mechanism to invoke methods on the server from JavaScript. It’s called Page Methods. Because those methods are static* you cannot use Page.Session property.  This does not mean that you have to give up the use of the session. Simply, you should use the HttpContext.Current.Session instead of Page.Session.

For instance, to save and read a value from session state, just use the code:

public static void SaveToSession(string s)
    HttpContext.Current.Session["abc"] = s;

public static string ReadFromSession()
    return (string)HttpContext.Current.Session["abc"];

And what if you want to get a logged user name? It is equally simple, use: HttpContext.Current.User.Identity.Name.

Access to session’s data is possible because when you send requests via the XmlHttpRequest object, the session ID cookie is also included. This cookie looks like this:

Cookie: ASP.NET_SessionId=kxxrwp45bhkyyhbzpijo0zj4

* Page Methods are static - and they should be! In this way, Page object creation is avoided and ViewState is not transmitted. The server has less work (no events such as Page_Load or Page_PreRender occur) and the amount of data sent over the network is significantly smaller.