ServerXMLHTTP appending to content-type

后端 未结 1 1219
一个人的身影
一个人的身影 2021-01-14 07:13

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

Set oXMLHttp = Server.CreateObject(\"MSXML2.ServerXMLHTTP\")
oXMLHttp.open cMe         


        
1条回答
  •  孤城傲影
    2021-01-14 07:35

    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

    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
    

    0 讨论(0)
提交回复
热议问题