ServerXMLHTTP 附加到内容类型 [英] ServerXMLHTTP appending to content-type

查看:19
本文介绍了ServerXMLHTTP 附加到内容类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making a server-side HTTP request with a JSON body in VBScript like this:

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.如您所见,我将上面的请求标头设置为这样.

The service I'm calling expects the content-type to be application/json, naturally. As you can see, I'm setting the request header above to be as such.

问题是 MSXML2.ServerXMLHTTP 会将一个字符集附加到我将内容类型设置为的任何内容(我找不到此行为的文档),默认值似乎是 UTF-8.所以最后,标头作为 application/json;Charset=UTF-8,web 服务不喜欢这个.

The issue is that MSXML2.ServerXMLHTTP will append a charset to whatever I set the content-type to (I can't find documentation for this behavior), with the default appearing to be UTF-8. So in the end, the header is sent as application/json; Charset=UTF-8, which the web service does not like.

奇怪的是,我可以用 setRequestHeader 显式设置一个字符集,甚至是一个无意义的字符集,然后 MSXML2.ServerXMLHTTP 将不理会标题.比如……

Odd thing is, I can explicitly set a charset with setRequestHeader, even a nonsensical one, and MSXML2.ServerXMLHTTP will then leave the header alone. For example..

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

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

Works fine and is left untouched. How can I stop MSXML2.ServerXMLHTTP from altering the content-type?

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

I found that MSXML2.ServerXMLHTTP 6.0 does not exhibit this behavior, at least by default. But I'd still like to see if there's a solution for this, as I'm not sure if that'll be available where this application needs to be installed.

设置 oXMLHttp = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")

推荐答案

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

According to the docs, this is an expected behavior of send method when the request body is string as yours. BTW I should say I can't reproduce the issue on Windows 10 and Windows Server 2008 R2 but Windows Server 2008. So the only thing I can say this behaviour must be valid for older builds of MSXML 3.0.

备注

如果输入类型是 BSTR,则响应始终编码为 UTF-8.调用者必须设置具有适当内容的 Content-Type 标头输入并包含一个字符集参数.

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.

如果输入类型是 XMLDOM 对象,则响应被编码根据 <? 上的编码属性中的 XML 声明文件.

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

如果没有 XML 声明或编码属性,则假定为 UTF-8.

如果输入类型是 UI1 的 SAFEARRAY,则按原样发送响应无需额外编码.调用者必须设置 Content-Type 标头具有适当的内容类型.

作为一种解决方法,您可以发送 cData 字节而不是指向变量.这对所有人都有效.

As a workaround, you can send bytes of cData instead of pointing the variable. This will work for all.

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

这篇关于ServerXMLHTTP 附加到内容类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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