Problem with CGI and .NET

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?
 
A

Arne Vajhøj

Jorge said:
I am trying to do a CGI in C#.
I have found code and explanation in
http://west-wind.com/weblog/posts/1143.aspx :
Console.Write(
@"HTTP/1.1 200 OK
Content-type: text/html

<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))
"

The code above is not correct CGI.

Console.Write(
@"Status: 200 OK
Content-type: text/html

<html>

should work.

Arne
 
J

Jorge Alves

Hi!
I have tried that modification and didn't work.
I also tried this:

Console.Write(
"Status 200 OK\r\nContent-type: text/html\r\n\r\n"+
"<html>Hello World"+
"</html>");

but it didn't also work.
I would like you try just this piece of code to see if it works (at least
with you).

Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top