gpt4 book ai didi

vbscript - 附加到内容类型的 ServerXMLHTTP

转载 作者:行者123 更新时间:2023-12-03 10:55:35 27 4
gpt4 key购买 nike

我正在使用 VBScript 中的 JSON 正文发出服务器端 HTTP 请求,如下所示:

Set oXMLHttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
oXMLHttp.open cMethod, cAPIURL, False, cUser, cPassword
oXMLHttp.setRequestHeader "Content-Type", "application/json"
oXMLHttp.send(cData)
cReturn = oXMLHttp.responseText
Set oXMLHttp = Nothing

我正在调用的服务自然希望内容类型为 application/json。如您所见,我将上面的请求 header 设置为这样。

问题是 MSXML2.ServerXMLHTTP 会将一个字符集附加到我将内容类型设置为的任何内容(我找不到此行为的文档),默认情况下似乎是 UTF- 8.所以最后,header 被发送为 application/json; Charset=UTF-8,网络服务不喜欢。

奇怪的是,我可以使用 setRequestHeader 显式设置一个字符集,即使是一个无意义的字符集,然后 MSXML2.ServerXMLHTTP 将单独保留 header 。例如..

oXMLHttp.setRequestHeader "Content-Type", "application/json; Charset=FOO"

工作正常并且保持不变。如何阻止 MSXML2.ServerXMLHTTP 更改内容类型?

编辑:我发现 MSXML2.ServerXMLHTTP 6.0 没有表现出这种行为,至少在默认情况下是这样。但我仍然想看看是否有解决方案,因为我不确定是否可以在需要安装此应用程序的地方使用。

Set oXMLHttp = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")

最佳答案

根据文档,这是 send 的预期行为当请求正文是你的字符串时的方法。顺便说一句,我应该说我无法在 Windows 10 和 Windows Server 2008 R2 上重现该问题,但在 Windows Server 2008 上重现。所以我唯一能说的就是这种行为必须对旧版本的 MSXML 3.0 有效。

Remarks

If the input type is a BSTR, the response is always encoded as UTF-8. The caller must set a Content-Type header with the appropriate content type and include a charset parameter.

If the input type is an XMLDOM object, the response is encoded according to the encoding attribute on the <? XML declaration in the document.

If there is no XML declaration or encoding attribute, UTF-8 is assumed.

If the input type is a SAFEARRAY of UI1, the response is sent as is without additional encoding. The caller must set a Content-Type header with the appropriate content type.

作为变通方法,您可以发送字节的 cData 而不是指向变量。这对所有人都有效。

Function Utf8BytesOf(text)
With Server.CreateObject("Adodb.Stream")
.Charset = "UTF-8"
.Open
.WriteText text
.Position = 0
.Type = 1 'adTypeBinary
.Read 3 'skip UTF-8 BOM
Utf8BytesOf = .Read 'read rest
.Close
End With
End Function

Set oXMLHttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
oXMLHttp.open cMethod, cAPIURL, False, cUser, cPassword
oXMLHttp.setRequestHeader "Content-Type", "application/json"
oXMLHttp.send Utf8BytesOf(cData)
cReturn = oXMLHttp.responseText
Set oXMLHttp = Nothing

关于vbscript - 附加到内容类型的 ServerXMLHTTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35022415/

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