ServerXMLHTTP 附加到 content-type
ServerXMLHTTP appending to content-type
我在 VBScript 中使用 JSON body 发出 server-side 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
我调用的服务自然希望 content-type 为 application/json
。如您所见,我将上面的请求 header 设置为这样。
问题是 MSXML2.ServerXMLHTTP
会将字符集附加到我将 content-type 设置为的任何内容(我找不到此行为的文档),默认值似乎是 UTF-8 .所以最后,header 被发送为 application/json; Charset=UTF-8
,Web 服务不喜欢它。
奇怪的是,我可以用 setRequestHeader
显式设置一个字符集,即使是一个无意义的字符集,然后 MSXML2.ServerXMLHTTP
将不理会 header。例如..
oXMLHttp.setRequestHeader "Content-Type", "application/json; Charset=FOO"
工作正常,没有受到影响。我怎样才能阻止 MSXML2.ServerXMLHTTP
改变 content-type?
编辑:我发现 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.
版本
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 中使用 JSON body 发出 server-side 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
我调用的服务自然希望 content-type 为 application/json
。如您所见,我将上面的请求 header 设置为这样。
问题是 MSXML2.ServerXMLHTTP
会将字符集附加到我将 content-type 设置为的任何内容(我找不到此行为的文档),默认值似乎是 UTF-8 .所以最后,header 被发送为 application/json; Charset=UTF-8
,Web 服务不喜欢它。
奇怪的是,我可以用 setRequestHeader
显式设置一个字符集,即使是一个无意义的字符集,然后 MSXML2.ServerXMLHTTP
将不理会 header。例如..
oXMLHttp.setRequestHeader "Content-Type", "application/json; Charset=FOO"
工作正常,没有受到影响。我怎样才能阻止 MSXML2.ServerXMLHTTP
改变 content-type?
编辑:我发现 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.
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