teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

誤記修正

2017/01/12 17:02

投稿

hikochang
hikochang

スコア648

answer CHANGED
@@ -7,7 +7,8 @@
7
7
  - エラー処理無し
8
8
  - 自前で頑張るお勧めしないコード
9
9
  - いろいろ問題あり
10
+ - 実際には関数仕様を確認すること
10
- - 実際には関数仕様を確認すること> using System.IO;
11
+ > using System.IO;
11
12
  > using System.Net;
12
13
  >
13
14
  > // http://127.0.0.1:8123 にアクセスしテキストを入力するとText.txtに内容が保存される

1

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

2017/01/12 17:01

投稿

hikochang
hikochang

スコア648

answer CHANGED
@@ -2,22 +2,47 @@
2
2
  シングルスレッドなのでLockなど不要。
3
3
  htmlの中では入力FROMでGETするようにするにして、Getされた時のQueryを読み取ってファイルに保存する。
4
4
 
5
+ - 最低限のコードだけ
6
+ - 動作確認済み
7
+ - エラー処理無し
8
+ - 自前で頑張るお勧めしないコード
9
+ - いろいろ問題あり
10
+ - 実際には関数仕様を確認すること> using System.IO;
11
+ > using System.Net;
12
+ >
13
+ > // http://127.0.0.1:8123 にアクセスしテキストを入力するとText.txtに内容が保存される
5
- > static void Main(string[] args)
14
+ > namespace WebLinkSample
6
15
  > {
16
+ > class Program
17
+ > {
7
- > string prefix = "http://*/";
18
+ > static void Main(string[] args)
19
+ > {
8
- > HttpListener listener = new HttpListener();
20
+ > HttpListener listener = new HttpListener();
9
- > listener.Prefixes.Add(prefix);
21
+ > listener.Prefixes.Add("http://+:8123/");
10
- > listener.Start();
22
+ > listener.Start();
23
+ > StreamWriter writer = new StreamWriter(@"Test.txt");
24
+ > while (true)
25
+ > {
26
+ > HttpListenerContext context = listener.GetContext();
27
+ > HttpListenerResponse res = context.Response;
11
28
  >
29
+ > switch (context.Request.Url.AbsolutePath)
30
+ > {
31
+ > case "/test.cgi":
32
+ > {
33
+ > StreamReader reader = new StreamReader(context.Request.InputStream);
34
+ > string str = reader.ReadToEnd();
35
+ > writer.WriteLine(str);
36
+ > writer.Flush();
37
+ > }
38
+ > break;
39
+ > }
40
+ > byte[] html = System.Text.Encoding.GetEncoding(932).GetBytes(@"<form action='test.cgi' method='post'><input type='text' name='name'></form>");
41
+ > context.Response.StatusCode = (int)HttpStatusCode.OK;
42
+ > context.Response.ContentType = "text/html";
43
+ > context.Response.OutputStream.Write(html, 0, html.Length);
44
+ > res.Close();
12
- > while (true)
45
+ > }
13
- > {
14
- > HttpListenerContext context = listener.GetContext();
15
- > HttpListenerRequest req = context.Request;
16
- > HttpListenerResponse res = context.Response;
17
- > switch( req.RawUrl )
18
- > {
46
+ > }
19
- > FORMでTextの内容をGETするように記載したhtmlを記載
20
- > GETコマンドのQueryStringからTextBoxの内容を取得しファイルに保存。
21
47
  > }
22
- > }
23
48
  > }