从 VBA 进行 UTF-8 调用 [英] Making a UTF-8 call from VBA
问题描述
I'm trying to make calls to openstreetmap (specifically to https://nominatim.openstreetmap.org/search?amenity=charging_station&format=json&q=Elly-Beinhorn-Ring+2,12529+Schönefeld ) and from any webbrowser it works fine. When calling via Excel VBA is complains that the string is not UTF-8. I haven't used fiddler yet but to me it is clear that srequest should be in UTF-8 format. I found another solution to convert the url to ascii but I'd prefer to have it in utf-8 here's some sample code which creates the error:
Dim httpObject As Object
Set httpObject = CreateObject("MSXML2.XMLHTTP")
srequest = "https://nominatim.openstreetmap.org/search?amenity=charging_station&format=json&q=Elly-Beinhorn-Ring+2,12529+Schönefeld"
httpObject.Open "GET", srequest, False
httpObject.setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
httpObject.send
sgetResult = httpObject.responsetext
Debug.Print sgetResult
如果有人可以帮助如何强制 MSXML2 进行 utf-8 调用,我会很高兴.目前我的解决方案是用 (ae,ue,oe) 替换德语元音变音 (ä,ü,ö),它有效但不是我想要的.URLEncode() 等其他解决方案也将 + 和 , 转换为以 % 开头的值,从而造成了更多混乱.
If anybody can help how to force MSXML2 to make an utf-8 call, i'd be happy. at the moment my solution is to replace German Umlauts (ä,ü,ö) with (ae,ue,oe) which works but is not what I want. Other solutions like URLEncode() have created more chaos by also converting + and , to values starting with %.
推荐答案
我可以通过替换这一行来解决我的问题:
I could solve my problem by replacing this line:
Set httpObject = CreateObject("MSXML2.XMLHTTP")
有了这个:
Set httpObject = CreateObject("MSXML2.ServerXMLHTTP")
这篇关于从 VBA 进行 UTF-8 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!