我在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^>
非常感谢。
答案 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^>