Esempio n. 1
0
        public async Task <DataLoadResult <string> > SynchronousLoadString(string url, params string[] endUrls)
        {
            this.SyncNavContext = new SynchronousNavigationContext
            {
                StartUrl = url,
                EndUrls  = endUrls,
                Tcs      = new TaskCompletionSource <SynchronousLoadResult>(),
            };
            var cts = new CancellationTokenSource();

            cts.CancelAfter(TimeSpan.FromSeconds(Debugger.IsAttached ? 15 : 10));
            using (cts.Token.Register(() => SyncNavContext.Tcs.TrySetCanceled(), useSynchronizationContext: false))
            {
                this.WB.Navigate(url);
                var result = await SyncNavContext.Tcs.Task;
                cts.Dispose();
                if (result.LoginRequired)
                {
                    return(new DataLoadResult <string>
                    {
                        LoginRequired = true
                    });
                }
                if (result.Success) // wait for DocumentCompleted
                {
                    if (result.IsWebBrowserResult)
                    {
                        return new DataLoadResult <string> {
                                   Data = WB.DocumentText
                        }
                    }
                    ;
                    else if (System.IO.File.Exists(result.FilePath))
                    {
                        var content = System.IO.File.ReadAllText(result.FilePath);

                        System.IO.File.Delete(result.FilePath);
                        return(new DataLoadResult <string> {
                            Data = content
                        });
                    }
                }
            }
            return(new DataLoadResult <string>
            {
                Data = string.Empty
            });
        }
Esempio n. 2
0
        public async Task <DataLoadResult <HtmlDocument> > SynchronousLoadDocument(string url, params string[] endUrls)
        {
            var cts = new CancellationTokenSource();

            cts.CancelAfter(TimeSpan.FromSeconds(Debugger.IsAttached ? 15 : 10));
            this.SyncNavContext = new SynchronousNavigationContext
            {
                StartUrl = url,
                EndUrls  = endUrls,
                Tcs      = new TaskCompletionSource <SynchronousLoadResult>(),
            };

            using (cts.Token.Register(() => SyncNavContext.Tcs.TrySetCanceled(), useSynchronizationContext: false))
            {
                this.WB.Navigate(url);
                var result = await SyncNavContext.Tcs.Task;
                if (result.LoginRequired)
                {
                    return(new DataLoadResult <HtmlDocument> {
                        LoginRequired = true
                    });
                }
                if (result.Success) // wait for DocumentCompleted
                {
                    if (result.IsWebBrowserResult)
                    {
                        return new DataLoadResult <HtmlDocument> {
                                   Data = WB.Document
                        }
                    }
                    ;
                    else
                    {
                        throw new NotSupportedException("无法将文件转换为HtmlElement");
                    }
                }
            }
            return(new DataLoadResult <HtmlDocument> {
                Data = null
            });
        }