{"id":2700,"date":"2019-03-22T02:05:29","date_gmt":"2019-03-21T17:05:29","guid":{"rendered":"http:\/\/okamurax.com\/?p=2700"},"modified":"2019-03-22T03:42:29","modified_gmt":"2019-03-21T18:42:29","slug":"c-%e3%82%b5%e3%83%bc%e3%83%90%e3%83%bc","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=2700","title":{"rendered":"C# \u30b5\u30fc\u30d0\u30fc"},"content":{"rendered":"<p>\u6700\u4f4e\u9650\u306e\u52d5\u4f5c\u3002<\/p>\n<pre class=\"lang:c# decode:true\">    public partial class Form1 : Form\r\n    {\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n\r\n            var httpListener = new HttpListener();\r\n            httpListener.Prefixes.Add(\"http:\/\/localhost:30000\/\");\r\n            httpListener.Start();\r\n\r\n            while (true)\r\n            {\r\n                var context = httpListener.GetContext();\r\n                var request = context.Request;\r\n                var response = context.Response;\r\n\r\n                byte[] content = Encoding.GetEncoding(\"shift_jis\").GetBytes(request.RawUrl.ToString());\r\n\r\n                try\r\n                {\r\n                    response.OutputStream.Write(content, 0, content.Length);\r\n                }\r\n                catch { }\r\n                finally\r\n                {\r\n                    response.Close();\r\n                }\r\n\r\n            }\r\n        }\r\n    }<\/pre>\n<p>\u3042\u308b\u3044\u306f\u3053\u3093\u306a\u66f8\u304d\u65b9<\/p>\n<pre class=\"lang:c# decode:true\">    public partial class Form1 : Form\r\n    {\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n\r\n            var httpListener = new HttpListener();\r\n            httpListener.Prefixes.Add(\"http:\/\/localhost:30000\/\");\r\n            httpListener.Start();\r\n\r\n            while (true)\r\n            {\r\n                httpListener.BeginGetContext(new AsyncCallback((result) =&gt; {\r\n\r\n                    HttpListener listener = (HttpListener)result.AsyncState;\r\n                    HttpListenerContext context = listener.EndGetContext(result);\r\n\r\n                    var request = context.Request;\r\n                    var response = context.Response;\r\n\r\n                    byte[] content = Encoding.GetEncoding(\"shift_jis\").GetBytes(request.RawUrl.ToString());\r\n\r\n                    response.OutputStream.Write(content, 0, content.Length);\r\n                    response.Close();\r\n\r\n                }), httpListener);\r\n            }\r\n            \r\n        }\r\n\r\n    }<\/pre>\n<p>\u30d5\u30a9\u30fc\u30e0\u3092\u4f7f\u3046\u5834\u5408<\/p>\n<pre class=\"lang:c# decode:true \">    public partial class Form1 : Form\r\n    {\r\n        HttpListener httpListener;\r\n\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n        }\r\n\r\n        private void button1_Click(object sender, EventArgs e)\r\n        {\r\n            if (httpListener != null) return;\r\n\r\n            this.Text = \"\u5f85\u53d7\u4e2d...\";\r\n\r\n            httpListener = new HttpListener();\r\n            httpListener.Prefixes.Add(\"http:\/\/localhost:30000\/\");\r\n\r\n            Task task = Task.Run(()=&gt; {\r\n\r\n                httpListener.Start();\r\n\r\n                while (true)\r\n                {\r\n                    var context = httpListener.GetContext();\r\n                    var request = context.Request;\r\n                    var response = context.Response;\r\n\r\n                    byte[] content = Encoding.GetEncoding(\"shift_jis\").GetBytes(request.RawUrl.ToString());\r\n\r\n                    response.OutputStream.Write(content, 0, content.Length);\r\n                    response.Close();\r\n\r\n                }\r\n\r\n            });\r\n\r\n        }\r\n\r\n        private void button2_Click(object sender, EventArgs e)\r\n        {\r\n            if (httpListener == null) return;\r\n\r\n            httpListener.Stop();\r\n            httpListener.Close();\r\n            httpListener = null;\r\n\r\n            this.Text = \"\u30c6\u30b9\u30c8\";\r\n        }\r\n    }<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u4f4e\u9650\u306e\u52d5\u4f5c\u3002 public partial class Form1 : Form { public Form1() { InitializeComponent(); var httpListener = new Htt &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=2700\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;C# \u30b5\u30fc\u30d0\u30fc&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[9],"class_list":["post-2700","post","type-post","status-publish","format-standard","hentry","category-1","tag-c-net"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2700","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2700"}],"version-history":[{"count":4,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2700\/revisions"}],"predecessor-version":[{"id":2704,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/2700\/revisions\/2704"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}