You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Windows Server 2022上通过POST请求运行CGI应用?

实现Web POST触发Windows Server本地程序的方法

方案一:用PowerShell搭建简易Web服务器(无需额外安装软件)

这个方案适合快速实现,不用配置复杂服务:

  1. 编写PowerShell脚本,保存为TriggerProgram.ps1:
$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add("http://+:8080/trigger/")
$listener.Start()

Write-Host "已启动监听,等待POST请求..."

while ($listener.IsListening) {
    $context = $listener.GetContext()
    $request = $context.Request
    $response = $context.Response

    # 解析表单数据
    $formData = @{}
    if ($request.ContentType -eq "application/x-www-form-urlencoded") {
        $body = New-Object System.IO.StreamReader($request.InputStream).ReadToEnd()
        $body.Split('&') | ForEach-Object {
            $keyValue = $_.Split('=')
            $formData[$keyValue[0]] = [System.Web.HttpUtility]::UrlDecode($keyValue[1])
        }
    }

    # 验证参数并执行程序
    if ($formData.ContainsKey('f') -and $formData.ContainsKey('b')) {
        $fParam = $formData['f']
        $bParam = $formData['b']
        # 注意:如果myprogram.exe不在系统PATH里,要写绝对路径,比如C:\apps\myprogram.exe
        Start-Process -FilePath "myprogram.exe" -ArgumentList "-f", $fParam, "-b", $bParam -NoNewWindow
        $response.StatusCode = 200
        $responseContent = "程序已成功触发执行"
    } else {
        $response.StatusCode = 400
        $responseContent = "错误:缺少必要参数f或b"
    }

    # 返回响应内容
    $buffer = [System.Text.Encoding]::UTF8.GetBytes($responseContent)
    $response.ContentLength64 = $buffer.Length
    $response.OutputStream.Write($buffer, 0, $buffer.Length)
    $response.Close()
}

$listener.Stop()
  1. 运行脚本:
  • 以管理员身份打开PowerShell
  • 执行Set-ExecutionPolicy RemoteSigned(允许运行本地脚本,按提示输入Y确认)
  • 切换到脚本所在目录,运行.\TriggerProgram.ps1
  1. 测试:
    用Postman、curl或其他工具发送POST请求到http://你的服务器IP:8080/trigger/,表单数据填写f=foo和b=bar即可触发程序。

方案二:用IIS+ASP脚本(适合已有IIS环境)

如果服务器已经部署了IIS,可直接用ASP脚本实现:

  1. 安装ASP组件:
    打开服务器管理器 -> 添加角色和功能 -> 选择Web服务器(IIS) -> 应用程序开发 -> 勾选ASP,完成安装。

  2. 配置网站:

  • 在IIS管理器中新建网站,设置站点物理路径(比如C:\WebTrigger),绑定端口(比如80)
  • 确保网站的应用程序池权限足够运行myprogram.exe(可暂时将应用程序池身份设为本地系统测试)
  1. 编写ASP脚本:
    在网站物理路径下新建default.asp,内容如下:
<%@ Language=VBScript %>
<%
    Dim f, b
    f = Request.Form("f")
    b = Request.Form("b")
    
    If f <> "" And b <> "" Then
        Dim WshShell
        Set WshShell = Server.CreateObject("WScript.Shell")
        ' 注意:myprogram.exe需写绝对路径或确保在系统PATH中
        WshShell.Run "myprogram.exe -f " & f & " -b " & b, 0, False
        Response.Write "程序已执行"
    Else
        Response.Write "错误:缺少参数f或b"
    End If
%>
  1. 测试:
    发送POST请求到网站的绑定地址,表单数据填写f=foo和b=bar即可。

注意事项

  • 如果myprogram.exe不在系统环境变量PATH中,所有脚本里的路径都要写绝对路径(比如C:\Programs\myprogram.exe)
  • PowerShell方案如果关闭窗口会停止监听,要后台运行可执行Start-Job -ScriptBlock { .\TriggerProgram.ps1 }

内容的提问来源于stack exchange,提问作者Fred

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 10:17:23