C# コンソールアプリを AN HTTPD で実行して WEB の根本を学ぶ

AN HTTP Server の使用方法

単純に、WEB アプリケーション初心者にブラウザとサーバーのやり取りを知ってもらう為に( 今は、開発者ツールがあるので途中の解説がしやすいですし )、C# で作った EXE をそのままブラウザで実行させます。

基本設定

EXE の列の『一般パスでも実行する』にチェックします



次に、一般パスに C# で作成された Debug フォルダを登録します。



後は、loclhost から実行するだけです。

ソースコード
001.using System;
002.using System.Collections;
003.using System.Text;
004.using System.Web;
005. 
006.namespace cgi_test
007.{
008.        class Program
009.        {
010.                static void Main(string[] args)
011.                {
012.                        string formtype = "POST";
013. 
014.                        string[] param = { };
015. 
016.                        // ******************************
017.                        // Console.WriteLine を UTF8 で
018.                        // ******************************
019.                        Console.OutputEncoding = Encoding.UTF8;
020. 
021.                        // ******************************
022.                        // メソッド
023.                        // ******************************
024.                        string method = Environment.GetEnvironmentVariable("REQUEST_METHOD");
025. 
026.                        // ******************************
027.                        // GET
028.                        // ******************************
029.                        if (method == "GET")
030.                        {
031.                                // ******************************
032.                                // QUERY_STRING
033.                                // ******************************
034.                                string query_string = System.Environment.GetEnvironmentVariable("QUERY_STRING");
035.                                // URL のみの場合はデータ無しの QUERY_STRING を用意する
036.                                if (query_string == "" )
037.                                {
038.                                        query_string = "field1=&field2=";
039.                                }
040. 
041.                                // & で分割して key=value の文字列の配列を作成する
042.                                param = query_string.Split('&');
043.                        }
044. 
045.                        // ******************************
046.                        // POST
047.                        // ******************************
048.                        if (method == "POST")
049.                        {
050.                                string line;
051. 
052.                                // POST 時は必ず key=value の文字列が存在する
053.                                line = Console.ReadLine();
054.                                param = line.Split('&');
055.                        }
056. 
057.                        // = で区切って key と value が配列の 0 と 1 にセットされる
058.                        string[] key_value = { };
059. 
060.                        // ******************************
061.                        // key と value の格納
062.                        // ******************************
063.                        Hashtable field = new Hashtable();
064. 
065.                        foreach (string key_value_set in param)
066.                        {
067.                                key_value = key_value_set.Split('=');
068.                                // key がある場合は、Hashtable に格納する
069.                                if (key_value[0] != "") {
070.                                        // System.Web を参照して using System.Web; で HttpUtility.UrlDecode
071.                                        // %エンコードを元に戻す
072.                                        field.Add(key_value[0], HttpUtility.UrlDecode(key_value[1]));
073.                                }
074.                        }
075. 
076.                        // ******************************
077.                        // HTTP ヘッダ
078.                        // PHP の session_cache_limiter
079.                        // ******************************
080.                        Console.WriteLine("Content-Type: text/html; charset=utf-8");
081.                        Console.WriteLine("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
082.                        Console.WriteLine("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
083.                        Console.WriteLine("Pragma: no-cache");
084.                        Console.WriteLine();
085. 
086.                        string message = "<div>こんにちは世界</div>";
087. 
088.                        // ******************************
089.                        // HTML
090.                        // $ で変数埋め込みのヒアドキュメント
091.                        // ******************************
092.                        string html = $@"<!DOCTYPE html>
093.<html>
094.<head>
095.</head>
096.<body>
097.{message}
098.<form method='{formtype}'>
099.<p>氏名 : <input type='text' name='field1' value='{field["field1"]}'></p>
100.<p>フリガナ : <input type='text' name='field2' value='{field["field2"]}'></p>
101.<p>送信 : <input type='submit' name='send' value='送信'></p>
102.</form>
103.</body>
104.</html>";
105. 
106.                        // 作成した HTML を出力する
107.                        Console.WriteLine(html);
108.                }
109.        }
110.}