【问题标题】:Classic ASP accessing an Array of Byte and converting it into a string经典 ASP 访问字节数组并将其转换为字符串
【发布时间】:2019-09-16 21:40:26
【问题描述】:

为了测试,我使用 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);

从那里我尝试将每个字节转换为一个字符并将其附加到一个字符串中,但是访问信息似乎是个问题。 这是我尝试过的一种尝试:

 var str = "";
 for (var x = 0; x < Request.TotalBytes; x++) {
     str += String.fromCharCode(byteArray[x]);
 }

当我在 Visual Studio 中签入时,数据如下所示:

有没有更好的方法从请求中获取数据?

【问题讨论】:

    标签: post asp-classic jscript


    【解决方案1】:

    访问字节数组并将其转换为字符串

    对于服务器端 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 对象从 Request 中写入二进制数据,将位置重置回流的开头,然后通过将二进制数据读取到变量中将其转换为正确编码的字符串 (发布数据)。如果 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);
    

    希望这有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-19
      • 2014-03-18
      • 2014-10-04
      相关资源
      最近更新 更多