CGI 的標準入出力


  C#



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

namespace WebApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //string formtype = "GET";
            string formtype = "POST";

            string[] param = {""};

            // ******************************
            // Console.WriteLine を UTF8 で
            // ******************************
            Console.OutputEncoding = Encoding.UTF8;

            // ******************************
            // メソッド
            // ******************************
            string method = Environment.GetEnvironmentVariable("REQUEST_METHOD");
            // ******************************
            // QUERY_STRING
            // ******************************
            string query_string = System.Environment.GetEnvironmentVariable("QUERY_STRING");

            // ******************************
            // HTTP ヘッダ
            // PHP の session_cache_limiter
            // ******************************
            Console.WriteLine("Content-Type: text/html; charset=utf-8");
            Console.WriteLine("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
            Console.WriteLine("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
            Console.WriteLine("Pragma: no-cache");
            Console.WriteLine();

            string message = "<div>こんにちは世界</div>";

            // ******************************
            // HTML
            // ******************************
            string html = @"<!DOCTYPE html>
<html>
<head>
</head>
<body>
#{message}
<form method='#{formtype}'>
<p>氏名 : <input type='text' name='field1' value='#{field1}'></p>
<p>フリガナ : <input type='text' name='field2' value='#{field2}'></p>
<p>送信 : <input type='submit' name='send' value='送信'></p>
</form>
</body>
</html>";
            html = html.Replace("#{formtype}", formtype);

            // ******************************
            // GET
            // ******************************
            if (method == "GET")
            {
                param = query_string.Split('&');
            }

            // ******************************
            // POST
            // ******************************
            if (method == "POST")
            {
                string line = null;
                line = Console.ReadLine();
                param = line.Split('&');
            }

            foreach (string set in param)
            {
                string[] key_value = set.Split('=');
                if (key_value[0] == "field1")
                {
                    // System.Web を参照して using System.Web;
                    key_value[1] = HttpUtility.UrlDecode(key_value[1]);
                    html = html.Replace("#{field1}", key_value[1]);
                }
                if (key_value[0] == "field2")
                {
                    key_value[1] = HttpUtility.UrlDecode(key_value[1]);
                    html = html.Replace("#{field2}", key_value[1]);
                }
            }


            html = html.Replace("#{field1}", "");
            html = html.Replace("#{field2}", "");


            // #{} は Ruby の真似
            html = html.Replace("#{message}", message);

            Console.WriteLine(html);


        }
    }
}
  










  infoboard   管理者用   





フリーフォントWEBサービス
SQLの窓WEBサービス

SQLの窓フリーソフト

素材

一般WEBツールリンク

SQLの窓

フリーソフト

JSライブラリ