0

I am attempting to write a C# cgi script that retrieves the value of an input field form on an HTML page I wrote. I read documentation on HttpRequest but I still can't get my code to compile

I have been google searching and nothing I have tried works. I have a very basic hello world C# cgi script which works. Here is the code which does not compile.

using System;
using System.Web;
using System.Web.UI;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
    static void Main(string[] args)
    {
        Console.WriteLine("Content-Type: text/html");
        Console.WriteLine("");
        Console.WriteLine("<HTML>");
        Console.WriteLine("<HEAD>");
        Console.WriteLine("<BODY>");
        Console.WriteLine("<H3>Hello from a csharp compiled CGI script</H3>");
        string mydata = Request["mydata"];
        Console.WriteLine("</BODY>");
        Console.WriteLine("</HTML>");
    }
    }
}

I get an error message as follows: error CS0103: The name 'Request' does not exist in the current context

  • 2
    You're getting the error because `Request` is not an instantiated object, nor a static class within your application. I believe it's possible you are a little confused about what you're trying to accomplish, and we certainly need more information in order to be helpful. If you're trying to make a request out to an external web service, you might want to look here: https://support.microsoft.com/en-us/help/307023/how-to-make-a-get-request-by-using-visual-c – Andy Stagg Oct 31 '19 at 17:08
  • Use a sniffer like wireshark or fiddler and compare the first request in the cgi with your current c# application. Make the request in c# look like cgi. – jdweng Oct 31 '19 at 17:11
  • Why not just use asp.net? – ADyson Oct 31 '19 at 17:18

3 Answers3

1

As others have said, Request is a proprietary thing from ASP.NET webpages and it doesn't even exists in plain console programs, even those implementing a CGI server. HttpRequest is also of no use, as CGI only interacts though STDIN, STDOUT and environrment variables.

In particular, POST data is normally sent though request body, and in the CGI standard, the body is feed to the program though STDIN, so you can read it using Console.Read() family of functions. Some other environment variables can also help you get more info about the request being served (Wikipedia reference).

Alejandro
  • 7,290
  • 4
  • 34
  • 59
0

Your app has no way of receiving an actual HTTP request. The answer provided by Jim would be good if you're looking to make a request and then parse the response, but otherwise you might look at creating an API project and logging out values to console/file.

a CGI (Common Gateway Interface) would be something typically listening for requests. Consider trying the asp.net API tutorials, which are also very well written, and would allow you to parse out something like Request["mydata"]

Evan Morrison
  • 652
  • 6
  • 18
  • That program actually is a bare bones CGI server, which is totally capable of receiving request, provided a web server forwards them to it. – Alejandro Oct 31 '19 at 17:22
-3

I think your looking for this type of request.

var request = System.Net.WebRequest.Create(requestPath);
            request.Timeout = 12000;
            request.Method = "GET";
            request.Headers.Add("TRN-Api-Key", apiKey);
            request.ContentType = "application/json";

            try
            {
                using (var response = request.GetResponse())
                {
                    using (var stream = response.GetResponseStream())
                    {
                        var reader = new StreamReader(stream, System.Text.Encoding.UTF8);
                        jString = reader.ReadToEnd();
                    }
                }

            }
            catch (System.Net.WebException e2)
            {
                //MessageBox.Show(e2.Message);
            }

I'm not sure what type of data exactly your looking for. This request here is specifically for JSON. You can also refer to this post as well by another user Get HTML code from website in C#