XMLHTTP 和特殊字符(例如,重音符号) [英] XMLHTTP and Special Characters (eg, accents)

查看:40
本文介绍了XMLHTTP 和特殊字符(例如,重音符号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 VBA 使用 Microsoft.XMLHTTP 来拉入网页的正文.这样做时,诸如 é 之类的字符会被替换为?".或者同样没用的东西.

I am using Microsoft.XMLHTTP via VBA to pull in the body of a web page. In doing so, characters such as é get replaced with "?" or something equally not useful.

这是基本代码:

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

有什么方法可以找回完整的特殊字符的页面吗?

Is there any way to retrieve the page with the special characters intact?

注意:我也尝试使用此请求标头但没有成功:
objHTTP.setRequestHeader "Content-Type", "content=text/html; charset=iso-8859-1"

Note: I have also tried using this request header with no success:
objHTTP.setRequestHeader "Content-Type", "content=text/html; charset=iso-8859-1"

提前致谢.

解决方案
感谢 Ben.Vineyard(以及一些粗略的谷歌搜索),我能够使用以下代码提取重音字符:

Solution
Thanks to Ben.Vineyard (and some cursory Googling), I'm able to pull accented characters with the following code:

 ' 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

推荐答案

问题可能是您实际上并未发送编码为 utf-8 的数据.它可能是 Ansi 或您使用的任何字符串/文件编码.然后它将无法在 ASCII 码中使用高于 127 的字符.你确定原始文本流是utf-8吗?您是否尝试过其他编码,例如其中一种 iso-* 格式?

The problem could be that you do not actually send the data encoded as utf-8. It might be in Ansi or whatever string/file encoding you use. And then it will not be able to use characters high than 127 in the ASCII code. Are you sure that the original text stream is utf-8? Have you tried other encoding like one of the iso-* formats?

这篇关于XMLHTTP 和特殊字符(例如,重音符号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆