使用ADODB.Stream保存POST二进制数据

Efe*_*Efe 2 ado asp-classic

另一个网站使用POST方法向我发送数据.

我想获取这些数据并将其插入数据库.

经过网上的一些研究,我得出结论,ADODB.Stream应该为我做的工作.

我没有获取二进制数据的问题Request.TotalBytes.使用以下代码,我没有收到错误,但它也没有保存数据.所以我必须对ADODB Stream做错事.

    tot_bytes = Request.TotalBytes

    Set BinaryStream = CreateObject("ADODB.Stream")
    BinaryStream.Mode = 3
    BinaryStream.Type = 1
    BinaryStream.Open       
    gBinaryData = BinaryStream.Write(tot_bytes) 
    BinaryStream.Close
    Set BinaryStream = Nothing

    SQL = "INSERT INTO STATUSES (StatusMessage, StatusDateEntered) VALUES ('"& gBinaryData &"', '"& FormatDateMySQL(NOW) &"')"                                  
    Set objAddC = objConn.execute(SQL)
Run Code Online (Sandbox Code Playgroud)

.

Following a successful subscription, Facebook will proceed to call your endpoint every time that there are changes (to the chosen fields or connections). For each update, it will make an HTTP POST request.

The request will have content type of application/json and the body will comprise a JSON-encoded string containing one or more changes.

Note for PHP developers: In PHP, to get the encoded data you would use the following code:

$data = file_get_contents("php://input");
$json = json_decode($data);
Run Code Online (Sandbox Code Playgroud)

Kul*_*gin 5

首先,Write方法不会返回任何内容,实际上它只是一个子例程.而Request.TotalBytes只是请求的长度,以字节为单位.当您需要将请求数据作为字节数组读取时,应使用Request.BinaryRead(字节长度).因此,在Stream对象中,您需要在写入字节并将设置位置设置为开始后使用Read方法读取所有字节.

但是,如果您必须将数据存储为二进制文件,则在这种情况下似乎不是必需的.
我假设你需要数据作为文本,很可能是json字符串.因此,您应该将数据从字节转换为字符串.

请注意,您需要处理有关总字节数的异常.如果请求不包含任何内容,则Request.TotalBytes等于零,并且由于Request.BinaryRead需要大于零且小于或等于总字节数的数字,因此会发生错误.

Dim tot_bytes, postData
    tot_bytes = Request.TotalBytes
If tot_bytes > 0 Then
    With Server.CreateObject("Adodb.Stream")
        .Charset = "utf-8" 'specify the request encoding
        .Type = 1 'adTypeBinary, a binary stream
        .Open
        .Write Request.BinaryRead(tot_bytes) 'Write bytes
        .Position = 0 ' set position to the start
        .Type = 2 ' adTypeText, stream type is text now
        postData = .ReadText 'read all text
        .Close
    End With
    With Server.CreateObject("Adodb.Recordset")
        .Open "STATUSES", objConn , , 3
        .AddNew
        .Fields("StatusMessage").Value = postData
        .Fields("StatusDateEntered").Value = Now()
        .Update
        .Close
    End With
    Response.Write "data stored successfully"
Else
    Response.Write "no post data"
End If
Run Code Online (Sandbox Code Playgroud)