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

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

问题描述

我想提出一个JSON的身体在VBScript这样的服务器端的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

我打电话给该服务预计内容类型为应用程序/ 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。所以,最后,头部被作为应用程序/ JSON的;字符集= 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

编辑:我发现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)

推荐答案

根据文档,这是的发送方法时,请求体是字符串作为你的。顺便说一句,我应该说我不能复制在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时,反应总是带codeD为UTF-8。
  调用者必须设置Content-Type头与相应的内容
  键入并包括charset参数。

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对象,响应EN codeD
  根据上的&lt ;?编码属性在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天全站免登陆