vbscript - 如何使用 Request.BinaryRead(Request.TotalBytes)

vbscript - How to use Request.BinaryRead(Request.TotalBytes)

我必须使用以下经典 ASP VBScript 代码:

binRequest = Request.BinaryRead(Request.TotalBytes)
Set objDomDoc = Server.CreateObject("Msxml2.DOMDocument.4.0")
bGoodXml = objDomDoc.load(binRequest)

此代码应该接收将转换为 xml 的内容。

我在 C# 中使用以下代码向包含上面列出的代码的页面发送带有 xml 的请求:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
string postData = getXML();
byte[] data = Encoding.ASCII.GetBytes(postData);<br/>
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = postData.Length;

using (Stream stream = req.GetRequestStream())
{
        stream.Write(data, 0, postData.Length);
}   
return "200";

private string getXML()
        {
            return "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>" +
                "<MessageHeader>" +
                    "<ApplicationNo>" +
                        "1325447" +
                    "</ApplicationNo>" +
                "</MessageHeader>";
        }

问题是在 VBScript 代码中 Request.TotalBytes 有一个值 (112),Request.Body 包含 xml 但 Request.BinaryRead(Request.TotalBytes) returns 为空并且bGoodXml 为假。

难道我做错了什么 ?我发送的数据不正确吗?不,我不能使用 Request.BodyobjDomDoc.loadXML(binRequest)。我必须使用给定的 VBScript 代码。

编辑:
忘了说我正在使用 .NET 2.0

.load reads data from a location/URL. Use .loadXML 从字符串中读取。

再想想:

您需要将从 .BinaryRead 返回的 SafeArray 转换为字符串。

第三次思考:

即使您 post 一个 URL,转换为 .load 需要的字符串也必须在 VBScript 代码中完成。

P.S。你的两个代码对我来说都很好。

首先,Request.BinaryRead 永远不会 returns Empty 但会导致错误。看起来代码中某处的 On Error Resume Next 抑制了错误,并且无法将值分配给 binRequest,因此您有 Empty。该错误必须与以下内容有关。

来自Request.BinaryRead Method

The BinaryRead method is used to read the raw data sent by the client as part of a POST request. This method is used for low-level access to this data, as opposed to, for example, using the Request.Form collection to view form data sent in a POST request. After you have used BinaryRead, referring to any variable in the Request.Form collection causes an error. Conversely, after you have referred to a variable in the Request.Form collection, using BinaryWrite will cause an error. Remember, if you access a variable in the Request collection without specifying which subcollection it belongs to, the Request.Form collection may be searched, bringing this rule into force.

我想你可以简单地这样做:

objDomDoc.load Request

其中 objDomDocMSXML2.DOMDOCUMENT 的实例,Request 是 ASP classic 的内置请求对象。