質問編集履歴
1
ソースコードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -151,3 +151,161 @@
|
|
151
151
|
</div>
|
152
152
|
|
153
153
|
```
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
現状の自身のソースコード
|
160
|
+
|
161
|
+
```
|
162
|
+
|
163
|
+
namespace webbrowser
|
164
|
+
|
165
|
+
{
|
166
|
+
|
167
|
+
public partial class Form1 : Form
|
168
|
+
|
169
|
+
{
|
170
|
+
|
171
|
+
private const string strRegPath = @"Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
|
172
|
+
|
173
|
+
private Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(strRegPath, true);
|
174
|
+
|
175
|
+
private string strProcessName = System.Diagnostics.Process.GetCurrentProcess().ProcessName + ".exe";
|
176
|
+
|
177
|
+
private CookieContainer cc;
|
178
|
+
|
179
|
+
public Form1()
|
180
|
+
|
181
|
+
{
|
182
|
+
|
183
|
+
InitializeComponent();
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
private void button1_Click(object sender, EventArgs e)
|
190
|
+
|
191
|
+
{
|
192
|
+
|
193
|
+
regKey.SetValue(strProcessName, 11001, Microsoft.Win32.RegistryValueKind.DWord);
|
194
|
+
|
195
|
+
webBrowser1.ScriptErrorsSuppressed = false;
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
webBrowser1.Visible = true;
|
200
|
+
|
201
|
+
webBrowser1.Navigate("機器のURL");//機器にアクセス
|
202
|
+
|
203
|
+
System.Threading.Thread.Sleep(2000);//2秒待つ
|
204
|
+
|
205
|
+
SendKeys.Send("Y");//「Y」を押す(セキュリティエラーの苦肉の回避策)
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
while (webBrowser1.IsBusy || webBrowser1.ReadyState != WebBrowserReadyState.Complete)
|
210
|
+
|
211
|
+
{
|
212
|
+
|
213
|
+
//無処理
|
214
|
+
|
215
|
+
System.Windows.Forms.Application.DoEvents();
|
216
|
+
|
217
|
+
System.Threading.Thread.Sleep(100);
|
218
|
+
|
219
|
+
}//開くまで待つ
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
HtmlWindow iframe = webBrowser1.Document.Window.Frames[0];
|
226
|
+
|
227
|
+
HtmlElementCollection elements = iframe.Document.All;
|
228
|
+
|
229
|
+
HtmlElement id = elements.GetElementsByName("userName")[0];
|
230
|
+
|
231
|
+
HtmlElement pw = elements.GetElementsByName("pwd")[0];
|
232
|
+
|
233
|
+
id.InnerText = "ID";//ログインIDを入力
|
234
|
+
|
235
|
+
pw.InnerText = "password";//ログインPASSを入力
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
HtmlElement login = elements.GetElementsByName("Submit")[0];
|
240
|
+
|
241
|
+
login.InvokeMember("click");//ログインボタンを押す
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
while (webBrowser1.IsBusy || webBrowser1.ReadyState != WebBrowserReadyState.Complete)
|
246
|
+
|
247
|
+
{
|
248
|
+
|
249
|
+
System.Windows.Forms.Application.DoEvents();
|
250
|
+
|
251
|
+
System.Threading.Thread.Sleep(100);
|
252
|
+
|
253
|
+
}//開くまで待つ
|
254
|
+
|
255
|
+
System.Threading.Thread.Sleep(2000);//2秒待つ
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
CookieContainer cc = new CookieContainer();
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
cc.SetCookies(webBrowser1.Url, webBrowser1.Document.Cookie.Replace(";", ","));
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("ダウンロード先URL");//Cookieをセットしてダウンロード先にアクセス
|
268
|
+
|
269
|
+
req.CookieContainer = cc;
|
270
|
+
|
271
|
+
/*下記でエラーが出る。
|
272
|
+
|
273
|
+
System.Net.WebException: '接続が切断されました: SSL/TLS のセキュリティで保護されているチャネルに対する信頼関係を確立できませんでした。
|
274
|
+
|
275
|
+
内部例外 AuthenticationException: 検証プロシージャによると、リモート証明書は無効です。
|
276
|
+
|
277
|
+
上記で停まってしまい、証明書をスルーするコードを記入すると何も取得出来なくなります。
|
278
|
+
|
279
|
+
*/
|
280
|
+
|
281
|
+
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
|
282
|
+
|
283
|
+
Stream st = res.GetResponseStream();
|
284
|
+
|
285
|
+
StreamReader sr = new StreamReader(st);
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
|
290
|
+
|
291
|
+
Stream st = res.GetResponseStream();
|
292
|
+
|
293
|
+
StreamReader sr = new StreamReader(st);
|
294
|
+
|
295
|
+
String htmlSorce = sr.ReadToEnd();
|
296
|
+
|
297
|
+
Console.WriteLine(htmlSorce);
|
298
|
+
|
299
|
+
sr.Close();
|
300
|
+
|
301
|
+
st.Close();
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
}
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
}
|
310
|
+
|
311
|
+
```
|