<code
class
=
"csharp"
>using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using mshtml;
namespace
HtmlExtractor
{
public
partial
class
MainForm : Form
{
public
MainForm()
{
SetFeatureBrowserEmulation();
InitializeComponent();
this.Load += MainForm_Load;
}
async void MainForm_Load(object sender, EventArgs e)
{
try
{
var
cts =
new
CancellationTokenSource(10000);
var
html = await LoadDynamicPage(
"https://www.google.com/#q=where+am+i"
, cts.Token);
MessageBox.Show(html.Substring(0, 1024) +
"..."
);
}
catch
(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
async Task<string> LoadDynamicPage(string url, CancellationToken token)
{
var
tcs =
new
TaskCompletionSource<bool>();
WebBrowserDocumentCompletedEventHandler handler = (s, arg) =>
tcs.TrySetResult(true);
using (token.Register(() => tcs.TrySetCanceled(), useSynchronizationContext: true))
{
this.webBrowser.DocumentCompleted += handler;
try
{
this.webBrowser.Navigate(url);
await tcs.Task;
}
finally
{
this.webBrowser.DocumentCompleted -= handler;
}
}
var
documentElement = this.webBrowser.Document.GetElementsByTagName(
"html"
)[0];
var
html = documentElement.OuterHtml;
while
(true)
{
await Task.Delay(500, token);
if
(this.webBrowser.IsBusy)
continue
;
var
htmlNow = documentElement.OuterHtml;
if
(html == htmlNow)
break
;
html = htmlNow;
}
token.ThrowIfCancellationRequested();
return
html;
}
static
void SetFeatureBrowserEmulation()
{
if
(LicenseManager.UsageMode != LicenseUsageMode.Runtime)
return
;
var
appName = System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
Registry.SetValue(@
"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION"
,
appName, 10000, RegistryValueKind.DWord);
}
}
}</code>