Post 经典ASP访问字节数组并将其转换为字符串
为了测试,我使用curl发送数据,如下所示:Post 经典ASP访问字节数组并将其转换为字符串,post,asp-classic,jscript,Post,Asp Classic,Jscript,为了测试,我使用curl发送数据,如下所示: curl -XPOST http://test.local/kms/test.asp --data "{\"a\":1212321}" -H 'Content-Type: application/json' 在test.asp中,我有如下内容: var byteArray = Request.BinaryRead(Request.TotalBytes); 从那里,我尝试将每个字节转换为一个字符,并将其附加到一个字符串中,但是访问信息似乎是个问题。
curl -XPOST http://test.local/kms/test.asp --data "{\"a\":1212321}" -H 'Content-Type: application/json'
在test.asp中,我有如下内容:
var byteArray = Request.BinaryRead(Request.TotalBytes);
从那里,我尝试将每个字节转换为一个字符,并将其附加到一个字符串中,但是访问信息似乎是个问题。
这是我尝试过的一次尝试:
var str = "";
for (var x = 0; x < Request.TotalBytes; x++) {
str += String.fromCharCode(byteArray[x]);
}
var-str=”“;
对于(var x=0;x
当我签入visual studio时,数据如下所示:
var byteArray = Request.BinaryRead(Request.TotalBytes);
有没有更好的方法从请求中获取数据
访问字节数组并将其转换为字符串
对于服务器端JScript,使用上面的curl POST示例,尝试以下操作:
var postData = null;
with (Server.CreateObject("ADODB.Stream")) {
Type = 1; // adTypeBinary
Open();
Write(Request.BinaryRead(Request.TotalBytes));
Position = 0;
Type = 2; // adTypeText
Charset = "iso-8859-1";
postData = ReadText();
Close();
}
if (postData) {
var json = null;
eval("json = " + postData);
Response.Write(json.a);
}
此解决方案使用ADODB.Stream对象写入请求中的二进制数据,将位置重置回流的开头,然后通过将二进制数据读入变量(postData)将其转换为正确编码的字符串。如果存在postData,请按原样计算JSON字符串,然后使用它
有没有更好的方法从请求中获取数据 也许吧。您可以将数据发布为“application/x-www-form-urlencoded”,然后使用Request.form(“a”)访问变量 例如,尝试将curl POST命令更改为:
curl -XPOST http://test.local/kms/test.asp --data "a=1212321" -H 'Content-Type:application/x-www-form-urlencoded'
然后更新服务器端ASP JScript代码,使其类似于:
var str = Request.Form("a");
Response.Write(str);
希望这是有帮助的