2

I was under the impression that I could put any old executable program in the cgi-bin directory of Apache and have it be used as a CGI script. Specifically, if I have a C# program

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);
    }
}

and then go to http://example.com/cgi-bin/TestProg?hello=kitty&goodbye=world then the query string hello=kitty&goodbye=world would be passed as the first parameter to main, so my page should look like

Arguments:
hello=kitty&goodbye=world

Unfortunately, none of my query parameters are getting passed; the page loads and just prints Arguments: with nothing following it.

So how do I get my query parameters passed to this program?

Eli Courtwright
  • 186,300
  • 67
  • 213
  • 256

2 Answers2

6

Arguments are not passed on the command line - instead, apache sets environment variables before the cgi program is called (http://httpd.apache.org/docs/2.0/howto/cgi.html#behindscenes).

You can access the environment variable 'QUERY_STRING' which contains the text of the query string.

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

You will then need to parse queryString yourself.

POST data, however, is passed over STDIN, so you will need to use Console.In to process it.

Alistair Evans
  • 36,057
  • 7
  • 42
  • 54
3

It has been a long time since I've worked with CGI and Apache, but if I recall correctly, the query string is an environment variable in Apache. In C#, you can see the environment with System.Environment.GetEnvironmentVariables. I don't have any published docs to back me up, but I'd try that out first and see.

Matt Hamsmith
  • 3,955
  • 1
  • 27
  • 42