質問編集履歴

1

インデントを修正、補足情報を追加

2023/01/18 02:12

投稿

ykcx
ykcx

スコア1

test CHANGED
File without changes
test CHANGED
@@ -17,39 +17,39 @@
17
17
  ```C#
18
18
  namespace ConsoleApplication1
19
19
  {
20
- public class Program
20
+ public class Program
21
- {
21
+ {
22
- static void Main()
22
+ static void Main()
23
- {
23
+ {
24
- // WebRequestによるCookie認証
24
+ // WebRequestによるCookie認証
25
- // POSTしてCookieContainerに書き込む
25
+ // POSTしてCookieContainerに書き込む
26
- var data = Encoding.ASCII.GetBytes(string.Join("&",
26
+ var data = Encoding.ASCII.GetBytes(string.Join("&",
27
- new[] { "id=guest", "pw=guest" }));
27
+ new[] { "id=guest", "pw=guest" }));
28
- var cookieContainer = new CookieContainer();
28
+ var cookieContainer = new CookieContainer();
29
- var req = (HttpWebRequest)WebRequest.Create("https://www.kent-web.com/pwd/protect/enter.cgi");
29
+ var req = (HttpWebRequest)WebRequest.Create("https://www.kent-web.com/pwd/protect/enter.cgi");
30
- req.CookieContainer = cookieContainer;
30
+ req.CookieContainer = cookieContainer;
31
- req.Method = "POST";
31
+ req.Method = "POST";
32
- req.ContentType = "application/x-www-form-urlencoded";
32
+ req.ContentType = "application/x-www-form-urlencoded";
33
- req.ContentLength = data.Length;
33
+ req.ContentLength = data.Length;
34
- using (var stream = req.GetRequestStream())
34
+ using (var stream = req.GetRequestStream())
35
- {
35
+ {
36
- stream.Write(data, 0, data.Length);
36
+ stream.Write(data, 0, data.Length);
37
- }
37
+ }
38
- var res = req.GetResponse(); // ここでCookieContainerに書き込まれる筈ですが、ダメです。
38
+ var res = req.GetResponse(); // ここでCookieContainerに書き込まれる筈ですが、ダメです。
39
39
 
40
- // 以下、そのCookieを使えばアクセス可能な筈ですが、、、。
40
+ // 以下、そのCookieを使えばアクセス可能な筈ですが、、、。
41
- var reqLog = (HttpWebRequest)WebRequest.Create("https://www.kent-web.com/pwd/protect/protect.cgi");
41
+ var reqLog = (HttpWebRequest)WebRequest.Create("https://www.kent-web.com/pwd/protect/protect.cgi");
42
- reqLog.CookieContainer = cookieContainer; // CookieContainerセット
42
+ reqLog.CookieContainer = cookieContainer; // CookieContainerセット
43
- var resLog = reqLog.GetResponse();
43
+ var resLog = reqLog.GetResponse();
44
- using (var stream = resLog.GetResponseStream())
44
+ using (var stream = resLog.GetResponseStream())
45
45
 
46
- using (var sr = new StreamReader(stream, Encoding.GetEncoding("shift-jis")))
46
+ using (var sr = new StreamReader(stream, Encoding.GetEncoding("shift-jis")))
47
- {
47
+ {
48
- Console.WriteLine(sr.ReadToEnd()); // アクセスできてるのを確認
48
+ Console.WriteLine(sr.ReadToEnd()); // アクセスできてるのを確認
49
- }
49
+ }
50
50
 
51
- }
51
+ }
52
- }
52
+ }
53
53
  }
54
54
  ```
55
55
 
@@ -60,5 +60,5 @@
60
60
 
61
61
  ### 補足情報(FW/ツールのバージョンなど)
62
62
 
63
- Visual Studio2019
63
+ Windows10の Visual Studio 2019 で.NET COREで実行できるアプリケーションを.NET 5.0で作成
64
64