ℹ️ The Auron SMS Server is now Auron Omni. Learn more here.

How can we help?

How can I handle Unicode request parameters in ASP classic?


Using Unicode request parameters in ASP Classic can be challenging. Whether you are creating SMS messages, sending emails, or adding a new record to a database using an ADO connection.

This is because request parameters are normally ASCII or UTF-8 encoded, which is the de facto standard on the internet. This does not work well with existing ActiveX or COM objects (such as ADODB or any of the Auron APIs). ActiveX or COM objects expect strings to be UTF-16 LE encoded.

Fortunately, there is a trick to get this right. You can encode your UTF-8 request parameter as UTF-16 LE before passing it to a COM object.

Like this:

Function Utf8ToUnicode(strText)
  Set objAdoStream = CreateObject("ADODB.Stream")
  objAdoStream.Open
  objAdoStream.Charset = "Windows-1252"

  objAdoStream.WriteText strText

  objAdoStream.Position = 0
  objAdoStream.Type = 2 ' adTypeText
  objAdoStream.Charset = "utf-8"

  Utf8ToUnicode = objAdoStream.ReadText(-1) 'adReadAll
 
  objAdoStream.Close
End Function

Now you can use this function to send COM object parameters like this:

' Get request parameters and encode them as UTF-16 LE
sBody = Utf8ToUnicode(Request("Body"))
sToAddress = Request("ToAddress")

' Create new message object and save to database
Set objMessageDb = CreateObject("AxMmServer.MessageDB")
Set objMessage = objMessageDb.Create("SMS")

' Set COM object properties
objMessage.ToAddress = sToAddress
objMessage.Body = sBody

' Save the message to the Auron Omni database
objMessageDb.Save objMessage
If objMessageDb.LastError <> 0 Then
  Response.Write "Error: " & _
    objMessageDB.GetErrorDescription(objMessageDB.LastError)
  Response.End
End If

Now you can fully support Unicode request parameters.