代码之家  ›  专栏  ›  技术社区  ›  Eli Courtwright

在Windows上将CGI参数传递给Apache中的可执行文件

  •  2
  • Eli Courtwright  · 技术社区  · 15 年前

    我觉得我可以在 cgi-bin Apache目录,并将其用作CGI脚本。具体来说,如果我有一个C程序

    static class TestProg
    {
        static void Main(string[] args)
        {
            Console.Write("Content-type: text/plain\r\n\r\n");
            Console.WriteLine("Arguments:");
            foreach (string arg in args)
                Console.WriteLine(arg);
        }
    }
    

    然后去 http://example.com/cgi-bin/TestProg?hello=kitty&goodbye=world 然后是查询字符串 hello=kitty&goodbye=world 将作为第一个参数传递给main,因此我的页面应该如下所示

    Arguments:
    hello=kitty&goodbye=world
    

    不幸的是,没有传递任何查询参数;页面加载并仅打印 Arguments: 没有任何跟踪。

    那么,如何将查询参数传递给这个程序呢?

    2 回复  |  直到 15 年前
        1
  •  4
  •   Alistair Evans    15 年前

    参数不会在命令行上传递-相反,Apache会在调用CGI程序之前设置环境变量。( http://httpd.apache.org/docs/2.0/howto/cgi.html#behindscenes )

    您可以访问包含查询字符串文本的环境变量'query_string'。

     String queryString = System.Environment.GetEnvironmentVariable("QUERY_STRING");
    

    然后您将需要自己分析querystring。

    但是,post数据是通过stdin传递的,所以您需要使用console.in来处理它。

        2
  •  3
  •   Matt Hamsmith Rahul Singh    15 年前

    我已经很久没有使用过CGI和Apache了,但是如果我能正确地回忆起,查询字符串是Apache中的一个环境变量。在C中,您可以看到带有System.Environment.GetEnvironmentVariables的环境。我没有任何已发布的文档来支持我,但我会先试试看。