回答編集履歴

2

誤記修正

2017/01/12 17:02

投稿

hikochang
hikochang

スコア648

test CHANGED
@@ -16,7 +16,9 @@
16
16
 
17
17
  - いろいろ問題あり
18
18
 
19
+ - 実際には関数仕様を確認すること
20
+
19
- - 実際には関数仕様を確認すること> using System.IO;
21
+ > using System.IO;
20
22
 
21
23
  > using System.Net;
22
24
 

1

実際に動作確認したソースコードに変更

2017/01/12 17:01

投稿

hikochang
hikochang

スコア648

test CHANGED
@@ -6,40 +6,90 @@
6
6
 
7
7
 
8
8
 
9
+ - 最低限のコードだけ
10
+
11
+ - 動作確認済み
12
+
13
+ - エラー処理無し
14
+
15
+ - 自前で頑張るお勧めしないコード
16
+
17
+ - いろいろ問題あり
18
+
19
+ - 実際には関数仕様を確認すること> using System.IO;
20
+
21
+ > using System.Net;
22
+
23
+ >
24
+
25
+ > // http://127.0.0.1:8123 にアクセスしテキストを入力するとText.txtに内容が保存される
26
+
9
- > static void Main(string[] args)
27
+ > namespace WebLinkSample
10
28
 
11
29
  > {
12
30
 
13
- > string prefix = "http://*/";
31
+ > class Program
14
32
 
15
- > HttpListener listener = new HttpListener();
33
+ > {
16
34
 
17
- > listener.Prefixes.Add(prefix);
35
+ > static void Main(string[] args)
18
36
 
37
+ > {
38
+
39
+ > HttpListener listener = new HttpListener();
40
+
41
+ > listener.Prefixes.Add("http://+:8123/");
42
+
19
- > listener.Start();
43
+ > listener.Start();
44
+
45
+ > StreamWriter writer = new StreamWriter(@"Test.txt");
46
+
47
+ > while (true)
48
+
49
+ > {
50
+
51
+ > HttpListenerContext context = listener.GetContext();
52
+
53
+ > HttpListenerResponse res = context.Response;
20
54
 
21
55
  >
22
56
 
23
- > while (true)
57
+ > switch (context.Request.Url.AbsolutePath)
24
58
 
25
- > {
59
+ > {
26
60
 
27
- > HttpListenerContext context = listener.GetContext();
61
+ > case "/test.cgi":
28
62
 
29
- > HttpListenerRequest req = context.Request;
63
+ > {
30
64
 
31
- > HttpListenerResponse res = context.Response;
65
+ > StreamReader reader = new StreamReader(context.Request.InputStream);
32
66
 
33
- > switch( req.RawUrl )
67
+ > string str = reader.ReadToEnd();
34
68
 
35
- > {
69
+ > writer.WriteLine(str);
36
70
 
37
- > FORMでTextの内容をGETするように記載したhtmlを記載
71
+ > writer.Flush();
38
72
 
73
+ > }
74
+
75
+ > break;
76
+
77
+ > }
78
+
79
+ > byte[] html = System.Text.Encoding.GetEncoding(932).GetBytes(@"<form action='test.cgi' method='post'><input type='text' name='name'></form>");
80
+
39
- > GETコマンドのQueryStringからTextBoxの内容を取得しファイルに保存。
81
+ > context.Response.StatusCode = (int)HttpStatusCode.OK;
82
+
83
+ > context.Response.ContentType = "text/html";
84
+
85
+ > context.Response.OutputStream.Write(html, 0, html.Length);
86
+
87
+ > res.Close();
88
+
89
+ > }
90
+
91
+ > }
40
92
 
41
93
  > }
42
94
 
43
- > }
44
-
45
95
  > }