从 VBA 进行 UTF-8 调用 [英] Making a UTF-8 call from VBA

查看:61
本文介绍了从 VBA 进行 UTF-8 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用 openstreetmap(特别是 https://nominatim.openstreetmap.org/search?amenity=charging_station&format=json&q=Elly-Beinhorn-Ring+2,12529+Schönefeld) 并且从任何网络浏览器它都可以正常工作.当通过 Excel VBA 调用时,抱怨该字符串不是 UTF-8.我还没有使用过 fiddler,但对我来说很明显 srequest 应该是 UTF-8 格式.我找到了另一种将 url 转换为 ascii 的解决方案,但我更喜欢将它放在 utf-8 中,这是一些导致错误的示例代码:

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屋!

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