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

質問編集履歴

4

ソースの変更

2018/11/22 03:43

投稿

warks1
warks1

スコア12

title CHANGED
File without changes
body CHANGED
@@ -24,29 +24,123 @@
24
24
 
25
25
 
26
26
  ```C#
27
+ # using は省略
28
+
29
+ namespace WindowsFormsApp
30
+ {
31
+ public partial class Form1 : Form
32
+ {
33
+ // キャンセレーショントークンソース
27
- private void loop(object sender, EventArgs e)
34
+ private CancellationTokenSource CancellationTokenSource;
35
+ //タイマーの宣言
36
+ System.Windows.Forms.Timer timer2 = new System.Windows.Forms.Timer();
37
+ public Form1()
28
38
  {
39
+ InitializeComponent();
40
+ //ボタン
41
+ button1.Enabled = true;
42
+ button2.Enabled = false;
43
+ //タイマー
44
+ loop(Main_loop);
45
+ }
46
+ public void button1_Click(object sender, EventArgs e)
47
+ {
48
+ //開始ボタンは有効・停止ボタンは無効
49
+ button1.Enabled = false;
50
+ button2.Enabled = true;
51
+
52
+ //タイマーのインターバル設定
53
+ timer2.Interval = intVal * 60000;
54
+ //タイマー有効化
55
+ timer2.Enabled = true;
56
+ }
57
+ private void button2_Click(object sender, EventArgs e)
58
+ {
59
+ //タイマーの停止
60
+ timer2.Enabled = false;
61
+
62
+ //開始ボタンは有効・停止ボタンは無効
63
+ button1.Enabled = true;
64
+ button2.Enabled = false;
65
+ }
66
+
67
+ public bool OpenWebWait()
68
+ {
29
69
  try
30
70
  {
71
+ //読み込み完了まで待つ
72
+ while (webBrowser1.IsBusy || webBrowser1.ReadyState != WebBrowserReadyState.Complete)
73
+ {
74
+ //無処理
75
+ System.Windows.Forms.Application.DoEvents();
76
+ System.Threading.Thread.Sleep(300);
77
+ }
78
+
79
+ return true;
80
+ }
81
+ catch (Exception)
82
+ {
83
+ return false;
84
+ }
85
+ }
86
+
87
+
88
+ //numericUpDown1 * 60秒 でループするタイマー
89
+ public void loop(EventHandler eventHandler)
90
+ {
91
+ timer2.Tick += new EventHandler(eventHandler);
92
+ timer2.Enabled = false;
93
+ }
94
+ private void Main_loop(object sender, EventArgs e)
95
+ {
96
+ try
97
+ {
98
+ //TLS1.2を使用・SSL自己証明書のスルー
99
+ ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(OnRemoteCertificateValidationCallback);
100
+ ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
101
+ //ファイルの保存先指定
102
+ string filepath = (保存先);
103
+
104
+ //label1の表示変更
31
-          label1.Text = "開始";
105
+ label1.Text = "接続中";
32
106
  label1.Refresh();
33
- webBrowser1.Visible = true;
34
- webBrowser1.Navigate("https://" + textBox01.Text + "." + textBox02.Text + "." + textBox03.Text + "." + textBox04.Text);
35
107
 
108
+
109
+ //接続
110
+ webBrowser1.Navigate(接続先URL);
111
+
36
112
  //ページの読み込み完了まで待つ
37
113
  OpenWebWait();
38
114
 
115
+ // クッキーを取得する。
116
+ string cookieStr = webBrowser1.Document.Cookie;
39
117
 
118
+ // WebClientを生成する。
40
119
  WebClient wc = new WebClient();
41
120
  Encoding enc = Encoding.UTF8;
121
+
122
+ // WebClientのヘッダ設定を行う。
42
- wc.DownloadFile("https://" + textBox01.Text + "." + textBox02.Text + "." + textBox03.Text + "." + textBox04.Text + "/txax_.txt", filepath);
123
+ wc.Headers[HttpRequestHeader.Cookie] = cookieStr;
124
+
125
+ //label1の表示変更
126
+ label1.Text = "ダウンロード中";
127
+ label1.Refresh();
128
+
129
+ //ダウンロード
130
+ wc.DownloadFile(ダウンロードURL);
43
131
  wc.Dispose();
132
+
133
+
134
+
44
135
  }
45
136
  catch (Exception w) when (w is ObjectDisposedException || w is InvalidOperationException || w is ArgumentOutOfRangeException || w is COMException)
46
137
  {
138
+ //label1の表示変更
47
139
  label1.Text = "接続エラー";
48
140
  label1.Refresh();
141
+
49
- Console.WriteLine(w.Message);
142
+ //Console.WriteLine(w.Message);
143
+ //1秒待つ
50
144
  Task.Delay(1000).Wait();
51
145
  }
52
146
  catch (Exception w) when (w is COMException)
@@ -55,23 +149,10 @@
55
149
  Application.Exit();
56
150
  }
57
151
  }
58
- public bool OpenWebWait()
152
+
59
- {
60
- try
61
- {
62
- //読み込み完了まで待つ
63
- while (webBrowser1.IsBusy || webBrowser1.ReadyState != WebBrowserReadyState.Complete)
64
- {
65
- //無処理
66
- System.Windows.Forms.Application.DoEvents();
67
- System.Threading.Thread.Sleep(100);
68
- }
69
153
 
70
- return true;
71
- }
72
- catch (Exception)
73
- {
74
- return false;
75
- }
76
154
  }
155
+ }
156
+ }
157
+
77
158
  ```

3

追加

2018/11/22 03:43

投稿

warks1
warks1

スコア12

title CHANGED
File without changes
body CHANGED
@@ -55,4 +55,23 @@
55
55
  Application.Exit();
56
56
  }
57
57
  }
58
+ public bool OpenWebWait()
59
+ {
60
+ try
61
+ {
62
+ //読み込み完了まで待つ
63
+ while (webBrowser1.IsBusy || webBrowser1.ReadyState != WebBrowserReadyState.Complete)
64
+ {
65
+ //無処理
66
+ System.Windows.Forms.Application.DoEvents();
67
+ System.Threading.Thread.Sleep(100);
68
+ }
69
+
70
+ return true;
71
+ }
72
+ catch (Exception)
73
+ {
74
+ return false;
75
+ }
76
+ }
58
77
  ```

2

質問の変更

2018/11/22 01:31

投稿

warks1
warks1

スコア12

title CHANGED
File without changes
body CHANGED
@@ -13,6 +13,16 @@
13
13
  原因・理由や改善方法等、出来るだけ初心者向けに回答していただけますと幸いです。
14
14
  お手数おかけしますが回答お願いいたします。
15
15
 
16
+ 追記
17
+ --------------------------------------
18
+ どんな例外処理が発生しても、その処理は終了して再度時間がきた際には1から「loop」の処理を開始したいです。
19
+ 例えば、物理的にLANケーブルが抜けていた場合、webbrowserコントロールでもDownloadFileでも例外が発生すると思います。その処理は停止します。
20
+ 途中でLANケーブルを挿しなおせば再度繋がるようになり、その後からタイマーの時間が来た際は例外処理が発生しないと思いますのでその際は普通に処理するという流れが良いです。
21
+
22
+ 現状では挿しなおした後も、例外処理が発生する状態です。
23
+ -------------------------------------------------
24
+
25
+
16
26
  ```C#
17
27
  private void loop(object sender, EventArgs e)
18
28
  {

1

変更

2018/11/22 01:29

投稿

warks1
warks1

スコア12

title CHANGED
File without changes
body CHANGED
@@ -26,6 +26,9 @@
26
26
  //ページの読み込み完了まで待つ
27
27
  OpenWebWait();
28
28
 
29
+
30
+ WebClient wc = new WebClient();
31
+ Encoding enc = Encoding.UTF8;
29
32
  wc.DownloadFile("https://" + textBox01.Text + "." + textBox02.Text + "." + textBox03.Text + "." + textBox04.Text + "/txax_.txt", filepath);
30
33
  wc.Dispose();
31
34
  }