Hi.
I am trying to do a CGI in C#.
I have found code and explanation in
http://west-wind.com/weblog/posts/1143.aspx :
using System;
using System.IO;
using System.Collecti ons;
using System.Text;
namespace CGITestApplicat ion
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class CGITest
{
/// <summary>
/// The main entry point for the application.
/// </summary>
///
[STAThread]
static void Main(string[] args)
{
// *** Use this for debugging –
// Hit the link then attach debugger to this process
// and then pause to continue
// System.Threadin g.Thread.Sleep( 30000);
// *** Loop through all the environement vars and write to
string
IDictionary Dict = Environment.Get EnvironmentVari ables();
StringBuilder sb = new System.Text.Str ingBuilder();
foreach (DictionaryEntr y Item in Dict)
{
sb.Append((stri ng)Item.Key + " - " + (string)Item.Va lue
+ "\r\n");
}
// *** Read individual values
string QueryString =
Environment.Get EnvironmentVari able("QUERY_STR ING");
// *** Read all the incoming form data both text and binary
string FormData = "";
byte[] Data = null;
///*
if (Environment.Ge tEnvironmentVar iable("REQUEST_ METHOD") ==
"POST")
{
Stream s = Console.OpenSta ndardInput();
BinaryReader br = new BinaryReader(s) ;
string Length =
Environment.Get EnvironmentVari able("CONTENT_L ENGTH");
int Size = Int32.Parse(Len gth);
Data = new byte[Size];
br.Read(Data, 0, Size);
// *** don’t close the reader!
FormData = System.Text.Enc oding.Default.G etString(Data,
0, Size);
}
//*/
Console.Write(
@"HTTP/1.1 200 OK
Content-type: text/html
<html>
Hello World
<pre>
<b>Environmen t and Server Variables:</b>
" + sb.ToString() + @"
<b>Form Vars (if any):</b>
" + FormData + @"
</pre>
</html>
");
}
}
}
I have compiled the code and put in a directory bellow wwwroot, with execute
permissions.
I have written the URL, and it didn't work; the answer was:
"
CGI Error
The specified CGI application misbehaved by not returning a complete set of
HTTP headers. The headers it did return are:
Unhandled Exception: System.Argument Exception: The parameter is incorrect.
(Exception from HRESULT: 0x80070057 (E_INVALIDARG))
"
When I try with unmanaged code (with a simpler source) I get the right answer.
Have you problems too with this C# code?