gpt4 book ai didi

vba - WinHttp.WinHttpRequest 添加到内容类型

转载 作者:行者123 更新时间:2023-12-04 08:42:08 26 4
gpt4 key购买 nike

我正在尝试使用 vba WinHttp.WinHttpRequest 向 kigo 的 api 发出请求,
我能够发送请求,但 WinHttpRequest 更改了内容类型添加
Charset=UTF-8 发送请求时,kigo 的 api 返回 415 错误。

我这样设置内容类型

web_Http.SetRequestHeader "Content-Type", "application/json"

但是当我在 Wireshark 中查看请求时,内容类型是这样的
Content-Type: application/json; Charset=UTF-8

有任何想法吗?

我找到了 this ,这与我的问题相似,但我不明白解决方案。

最佳答案

我也遇到了这个问题。似乎仅限于 WinHttp.WinHttpRequest 通讯接口(interface)。有几个不同的选项可以解决这个问题。

经过一番挖掘,我找到了this post来自微软员工。它给出了清晰的解释并建议发送二进制数组。

If you are POSTing a string using the WinHttpRequest object, you cannot override how it encodes the string for transmission. The WinHttpRequest object will always convert the Unicode string to UTF-8.

However, note that a Unicode string that contains only 7-bit LATIN-1/ISO-8859-1 characters will remain unchanged when encoded as UTF-8 ;-) In such cases, the WinHttpRequest object does not append a "Charset=UTF-8" attribute to your Content-Type header. (And I would think that the server would assume that the POST data is ISO-8859-1.)

So, if the XML text data that you are POSTing contains LATIN-1 alphanumeric or punctuation character codes (each less than decimal 128), then all you should have to do is specify the "ISO-8859-1" charset in your Content-Type header:

WinHttpReq.SetRequestHeader "Content-Type", "application/xml;Charset=ISO-8859-1"

However, if your POST data contains 8-bit characters, then you cannot supply the data as a string to the Send method. In order to avoid the UTF-8 conversion, your application must convert the string into a byte array, and supply that instead. The WinHttpRequest object will not attempt any data conversion on a byte array.

Regards,

Stephen Sulzer

Microsoft Corporation



除了发送二进制数组之外,第二个选项是切换到 Msxml2.XMLHTTP 。或 Msxml2.ServerXMLHTTP .这些都不会破坏 Content-Type 标题。幸运的是,当 WinHttp.WinHttpRequest创建时,Microsoft 有意使用 Msxml2.XMLHTTP作为界面模板。因此,转换代码相当简单。

此外, Msxml2.ServerXMLHTTP COM接口(interface) uses WinHTTP internally .因此,当您无法 Access WinHttp.WinHttpRequest 独有的某些功能时,两者都使用相同的后端。

第三个选项是使用 ADODB.Stream .它允许您使用 IStream ,这不是您通常可以从 VBA 执行的操作。下面的示例代码基于问题 "How to create BinaryArray in VbScript?" 的答案。 .
' Create a Binary Stream
Set objStreamBinary = CreateObject("ADODB.Stream")
objStreamBinary.Type = 1
objStreamBinary.Open

' Create a Text Stream
Set objStreamText = CreateObject("ADODB.Stream")
objStreamText.Type = 2
objStreamText.Open
' Copy the POST data to the Text Stream
objStreamText.WriteText strRequest
objStreamText.Position = 2
' Copy the Text Stream Contents to the Binary Stream
objStreamText.CopyTo objStreamBinary
objStreamText.Close

' Read the contents of the Binary Stream
' and send it to the WinHttpRequest object
web_Http.Send objStreamBinary.Read(-1)

关于vba - WinHttp.WinHttpRequest 添加到内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35641883/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com