Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

vba - XMLHTTP and Special Characters (eg, accents)

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.

Here's the basic code:

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?

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

Thanks in advance.

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...