How can we help?

How can I handle Unicode request parameters in ASP classic?


Using Unicode request parameters in ASP classic can be challenge. Whether you want to create SMS messages, send e-mails or just add 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 factor standard on the internet. This does not work well with existing ActiveX or COM objects (Such as ADODB or any of the Auron API’s). ActiveX or COM objects expect strings to be UTF-16 LE encoded.

Fortunately there’s a trick to get this right. Just encode you UTF-8 request parameter as UTF-16 LE before feeding them to a COM object.

Like so:

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 to sent COM object parameters like this:

' Get request parameters and encode them as UTF16 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 SMS Server database
objMessageDb.Save objMessage
If objMessageDb.LastError <> 0 Then
  Response.Write "Error: " & _
    objMessageDB.GetErrorDescription(objMessageDB.LastError)
  Response.End
End If

Now you’re fully able to support Unicode request parameters.