ServerXMLHTTP appending to content-type

2019-02-18 15:40发布

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

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.

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.

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"

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

EDIT: 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.

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

1条回答
Viruses.
2楼-- · 2019-02-18 16:23

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.

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.

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
查看更多
登录 后发表回答