var*_*ant 6 vba xmlhttprequest character-encoding special-characters
我通过VBA使用Microsoft.XMLHTTP来引入网页的主体.在这样做时,é等字符会替换为"?" 或同样没用的东西.
这是基本代码:
Set objHTTP = CreateObject("Microsoft.XMLHTTP")
objHTTP.Open "GET", ThisWebPage, False
objHTTP.setRequestHeader "Content-Type", _
"application/x-www-form-urlencoded; charset=UTF-8"
objHTTP.Send ("")
strResponse = objHTTP.responseText
Run Code Online (Sandbox Code Playgroud)
有没有办法检索特殊字符完整的页面?
注意:我也尝试使用此请求标头但没有成功:
objHTTP.setRequestHeader "Content-Type", "content=text/html; charset=iso-8859-1"
提前致谢.
解决方案
感谢Ben.Vineyard(以及一些粗略的Googling),我可以使用以下代码拉出重音字符:
' Create the XMLHTTP object
Set objHTTP = CreateObject("Microsoft.XMLHTTP")
' Send the request
objHTTP.Open "GET", WhatWebPage, False
objHTTP.Send ("")
Dim BinaryStream
Set BinaryStream = CreateObject("ADODB.Stream")
With BinaryStream
.Type = adTypeBinary
.Open
.Write objHTTP.ResponseBody
'Change stream type To binary
.Position = 0
.Type = adTypeText
'Specify charset For the source text (unicode) data.
.Charset = "iso-8859-1"
'Open the stream And get binary data from the object
strResponse = .ReadText
End With
Run Code Online (Sandbox Code Playgroud)
问题可能是您实际上并未发送编码为 utf-8 的数据。它可能采用 Ansi 或您使用的任何字符串/文件编码。那么它将无法使用 ASCII 码中高于 127 的字符。你确定原始文本流是utf-8吗?您是否尝试过其他编码,例如 iso-* 格式之一?