如何将获取方法转换为.bat文件中的post方法虽然我在服务器下使用它作为cgi?

时间:2014-05-19 03:47:04

标签: batch-file cmd cgi

我在apache下使用cmd作为cgi,但我没有得到一个很好的教程。你能解释一下如何将这个脚本从GET方法转换为POST方法吗?

echo.
rem *** Body begins here
echo ^<html^>^<body^>
echo ^<p^>This page contains short descriptions of some batch file commands. Click each   name to view the contents.^</p^>^<dl^>
echo ^<dt^>^<a href="?CALL"^>CALL^</a^>^</dt^>
if "%QUERY_STRING%"=="CALL" echo ^<dd^>Calls one batch program from another without causing the parent batch program to stop.^</dd^>
echo ^</body^>^</html^>

非常感谢。

2 个答案:

答案 0 :(得分:1)

在GET请求的情况下,发送的信息位于QUERY_STRING环境变量中。在POST请求的情况下,发送的信息写在cgi的标准输入中。因此,要使其适应处理POST,您需要从stdin检索信息并处理它。像

这样的东西
if "%REQUEST_METHOD%"=="POST" (
    for /f "delims=" %%a in ('findstr "^"') do (
        echo(^<div^>%%a^</div^>
    )
)

在此代码中,为了处理传入数据,使用for /f循环。它使用findstr命令获取其数据,该命令没有指示文件,将从stdin读取数据。当findstr检索到所有数据后,for循环将逐行开始处理。在此示例中,为每个检索的行生成<div>

请记住,这是一个批处理文件,指定的代码仅用于处理文本数据,并且具有批处理文件的常见限制:最大行长度为8191个字符,findstr检索的所有数据都需要在开始处理之前存储在内存中。

答案 1 :(得分:-1)

echo.
rem *** Body begins here
echo ^<html^>^<body^>
echo ^<p^>This page contains short descriptions of some batch file commands. Click each   name to view the contents.^</p^>^<dl^>
echo ^<dt^>^<a href="?CALL"^>CALL^</a^>^</dt^>
if "%QUERY_STRING%"=="CALL" echo ^<dd^>Calls one batch program from another without causing the parent batch program to stop.^</dd^>

使用%%1代替%QUERY_STRING%

echo ^</body^>^</html^>
从我读到的内容来看,客户端正在请求此页面:

https://foo.bar.com/mywebsite/cgi-bin/myscript.bat?call

如果客户是这样请求的话:

https://foo.bar.com/mywebsite/cgi-bin/myscript.bat?request=call

然后你的代码就可以了。几乎。请参阅,当客户端请求类似的内容时:?call脚本会自动接收call并将其存储为参数。在这种情况下,%1

得到它, 请改用:

echo.
rem *** Body begins here
echo ^<html^>^<body^>
echo ^<p^>This page contains short descriptions of some batch file commands. Click each   name to view the contents.^</p^>^<dl^>
echo ^<dt^>^<a href="?CALL"^>CALL^</a^>^</dt^>
if "%%1"=="CALL" echo ^<dd^>Calls one batch program from another without causing the parent batch program to stop.^</dd^>
echo ^</body^>^</html^>