我有一个 Web 服务加载驱动程序,它是 Windows 脚本文件 (WSF),其中包括一些 VBScript 和 JavaScript 文件。我的 Web 服务要求传入消息采用 Base64 编码。我目前有一个 VBScript 函数可以执行此操作,但效率非常低(内存密集型,主要是由于 VBScript 糟糕的字符串连接)
[旁白;是的,我见过Jeff's latest blog post 。连接发生在大小为 1,000 到 10,000 字节的消息之间的循环中。]
我尝试过使用一些自定义字符串连接例程;一种使用数组,一种使用 ADODB.Stream。这些有一点帮助,但我认为如果我有其他方法来编码消息而不是通过我自己的 VBS 函数,会更有帮助。
是否有其他方式对我的消息进行编码,最好使用 native Windows 方法?
最佳答案
我最初使用的是 Antonin Foller 的一些 VBScript 代码: Base64 Encode VBS Function和 Base64 Decode VBS Function 。
搜索 Antonin 的网站,我看到他有一些 quoted printable encoding, using the CDO.Message object 的代码,所以我尝试了。
最后,我将Mark的回答中提到的代码移植到VBScript(还使用了this SO问题中的一些代码),并使用了Stream___StringToBinary和 Stream_BinaryToString来自 Antonin 网站的函数来获取使用 MSXML 编码的函数。
我进行了一项快速测试,测量所有四种方法中 1,500 个字符消息的编码时间(我需要发送到网络服务的平均消息大小):
- native VBScript (VBScript)
- 引用可打印,使用 CDO.Message (QP)
- 引用的可打印二进制文件,使用 CDO.Message(QP 二进制文件)
- MSXML/ADODB.Stream (MSXML)
结果如下:
Iterations : 10,000 Message Size : 1,500 +-------------+-----------+ + Method | Time (ms) + +-------------+-----------+ | VBScript | 301,391 | +-------------+-----------+ | QP | 12,922 | +-------------+-----------+ | QP (Binary) | 13,953 | +-------------+-----------+ | MSXML | 3,312 | +-------------+-----------+
I also monitored the memory utilization (Mem Usage for the cscript.exe process in the Windows Task Manager) while the test was running. I don't have any raw numbers, but the memory utilization for both the quoted printable and MSXML solutions were below the VBScript solution (7,000K for the former, around 16,000K for VBScript).
I decided to go with the MSXML solution for my driver. For those interested, here's the code I'm using:
base64.vbs
Function Base64Encode(sText)
Dim oXML, oNode
Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
Set oNode = oXML.CreateElement("base64")
oNode.dataType = "bin.base64"
oNode.nodeTypedValue =Stream_StringToBinary(sText)
Base64Encode = oNode.text
Set oNode = Nothing
Set oXML = Nothing
End Function
Function Base64Decode(ByVal vCode)
Dim oXML, oNode
Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
Set oNode = oXML.CreateElement("base64")
oNode.dataType = "bin.base64"
oNode.text = vCode
Base64Decode = Stream_BinaryToString(oNode.nodeTypedValue)
Set oNode = Nothing
Set oXML = Nothing
End Function
'Stream_StringToBinary Function
'2003 Antonin Foller, http://www.motobit.com
'Text - string parameter To convert To binary data
Function Stream_StringToBinary(Text)
Const adTypeText = 2
Const adTypeBinary = 1
'Create Stream object
Dim BinaryStream 'As New Stream
Set BinaryStream = CreateObject("ADODB.Stream")
'Specify stream type - we want To save text/string data.
BinaryStream.Type = adTypeText
'Specify charset For the source text (unicode) data.
BinaryStream.CharSet = "us-ascii"
'Open the stream And write text/string data To the object
BinaryStream.Open
BinaryStream.WriteText Text
'Change stream type To binary
BinaryStream.Position = 0
BinaryStream.Type = adTypeBinary
'Ignore first two bytes - sign of
BinaryStream.Position = 0
'Open the stream And get binary data from the object
Stream_StringToBinary = BinaryStream.Read
Set BinaryStream = Nothing
End Function
'Stream_BinaryToString Function
'2003 Antonin Foller, http://www.motobit.com
'Binary - VT_UI1 | VT_ARRAY data To convert To a string
Function Stream_BinaryToString(Binary)
Const adTypeText = 2
Const adTypeBinary = 1
'Create Stream object
Dim BinaryStream 'As New Stream
Set BinaryStream = CreateObject("ADODB.Stream")
'Specify stream type - we want To save binary data.
BinaryStream.Type = adTypeBinary
'Open the stream And write binary data To the object
BinaryStream.Open
BinaryStream.Write Binary
'Change stream type To text/string
BinaryStream.Position = 0
BinaryStream.Type = adTypeText
'Specify charset For the output text (unicode) data.
BinaryStream.CharSet = "us-ascii"
'Open the stream And get text/string data from the object
Stream_BinaryToString = BinaryStream.ReadText
Set BinaryStream = Nothing
End Function
关于encoding - 在 VBScript 中对字符串进行 Base64 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/496751/