J
Jorge Alves
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.Collections;
using System.Text;
namespace CGITestApplication
{
/// <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.Threading.Thread.Sleep(30000);
// *** Loop through all the environement vars and write to
string
IDictionary Dict = Environment.GetEnvironmentVariables();
StringBuilder sb = new System.Text.StringBuilder();
foreach (DictionaryEntry Item in Dict)
{
sb.Append((string)Item.Key + " - " + (string)Item.Value
+ "\r\n");
}
// *** Read individual values
string QueryString =
Environment.GetEnvironmentVariable("QUERY_STRING");
// *** Read all the incoming form data both text and binary
string FormData = "";
byte[] Data = null;
///*
if (Environment.GetEnvironmentVariable("REQUEST_METHOD") ==
"POST")
{
Stream s = Console.OpenStandardInput();
BinaryReader br = new BinaryReader(s);
string Length =
Environment.GetEnvironmentVariable("CONTENT_LENGTH");
int Size = Int32.Parse(Length);
Data = new byte[Size];
br.Read(Data, 0, Size);
// *** don’t close the reader!
FormData = System.Text.Encoding.Default.GetString(Data,
0, Size);
}
//*/
Console.Write(
@"HTTP/1.1 200 OK
Content-type: text/html
<html>
Hello World
<pre>
<b>Environment 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.ArgumentException: 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?
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.Collections;
using System.Text;
namespace CGITestApplication
{
/// <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.Threading.Thread.Sleep(30000);
// *** Loop through all the environement vars and write to
string
IDictionary Dict = Environment.GetEnvironmentVariables();
StringBuilder sb = new System.Text.StringBuilder();
foreach (DictionaryEntry Item in Dict)
{
sb.Append((string)Item.Key + " - " + (string)Item.Value
+ "\r\n");
}
// *** Read individual values
string QueryString =
Environment.GetEnvironmentVariable("QUERY_STRING");
// *** Read all the incoming form data both text and binary
string FormData = "";
byte[] Data = null;
///*
if (Environment.GetEnvironmentVariable("REQUEST_METHOD") ==
"POST")
{
Stream s = Console.OpenStandardInput();
BinaryReader br = new BinaryReader(s);
string Length =
Environment.GetEnvironmentVariable("CONTENT_LENGTH");
int Size = Int32.Parse(Length);
Data = new byte[Size];
br.Read(Data, 0, Size);
// *** don’t close the reader!
FormData = System.Text.Encoding.Default.GetString(Data,
0, Size);
}
//*/
Console.Write(
@"HTTP/1.1 200 OK
Content-type: text/html
<html>
Hello World
<pre>
<b>Environment 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.ArgumentException: 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?