Exemplo n.º 1
0
        /// <summary>
        /// Loads the given URI by using an asynchronous request with the given
        /// method and body.
        /// </summary>
        /// <param name="loader">The document loader to use.</param>
        /// <param name="request">The request to issue.</param>
        /// <param name="cancel">
        /// The token which can be used to cancel the request.
        /// </param>
        /// <returns>
        /// The task which will eventually return the response.
        /// </returns>
        public static Task<IResponse> SendAsync(this IDocumentLoader loader, DocumentRequest request, CancellationToken cancel)
        {
            if (loader == null)
                return DefaultResponse;

            return loader.LoadAsync(request, cancel);
        }
Exemplo n.º 2
0
 public static DocumentRequest Build(string uri)
 {
     var documentRequest = new DocumentRequest(new Url(uri));
     documentRequest.Headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
     documentRequest.Headers["Accept-Charset"] = "utf-8";
     documentRequest.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36";
     return documentRequest;
 }
Exemplo n.º 3
0
        /// <summary>
        /// Loads the given URI by using an asynchronous request with the given
        /// method and body.
        /// </summary>
        /// <param name="loader">The document loader to use.</param>
        /// <param name="request">The request to issue.</param>
        /// <param name="cancel">The cancellation token.</param>
        /// <returns>
        /// The task which will eventually return the response.
        /// </returns>
        public static Task<IResponse> SendAsync(this IDocumentLoader loader, DocumentRequest request, CancellationToken cancel)
        {
            if (loader != null)
            {
                var download = loader.DownloadAsync(request);
                cancel.Register(download.Cancel);
                return download.Task;
            }

            return TaskEx.FromResult(default(IResponse));
        }
        /// <summary>
        /// Opens a new document loaded from the specified request
        /// asynchronously in the given context.
        /// </summary>
        /// <param name="context">The browsing context to use.</param>
        /// <param name="request">The request to issue.</param>
        /// <param name="cancel">The cancellation token.</param>
        /// <returns>The task that creates the document.</returns>
        public static async Task<IDocument> OpenAsync(this IBrowsingContext context, DocumentRequest request, CancellationToken cancel)
        {
            if (request == null)
                throw new ArgumentNullException("request");

            using (var response = await context.Loader.SendAsync(request, cancel).ConfigureAwait(false))
            {
                if (response != null)
                    return await context.OpenAsync(response, cancel).ConfigureAwait(false);
            }

            return await context.OpenNewAsync(request.Target.Href).ConfigureAwait(false);
        }
        /// <summary>
        /// Opens a new document loaded from the specified request
        /// asynchronously in the given context.
        /// </summary>
        /// <param name="context">The browsing context to use.</param>
        /// <param name="request">The request to issue.</param>
        /// <param name="cancel">The cancellation token.</param>
        /// <returns>The task that creates the document.</returns>
        public static async Task<IDocument> OpenAsync(this IBrowsingContext context, DocumentRequest request, CancellationToken cancel)
        {
            if (request == null)
                throw new ArgumentNullException(nameof(request));

            var loader = context.Loader;

            if (loader != null)
            {
                var download = loader.DownloadAsync(request);
                cancel.Register(download.Cancel);

                using (var response = await download.Task.ConfigureAwait(false))
                {
                    if (response != null)
                    {
                        return await context.OpenAsync(response, cancel).ConfigureAwait(false);
                    }
                }
            }

            return await context.OpenNewAsync(request.Target.Href).ConfigureAwait(false);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Loads the given URI by using an asynchronous request with the given
 /// method and body.
 /// </summary>
 /// <param name="loader">The document loader to use.</param>
 /// <param name="request">The request to issue.</param>
 /// <param name="cancel">
 /// The token which can be used to cancel the request.
 /// </param>
 /// <returns>
 /// The task which will eventually return the response.
 /// </returns>
 public static Task<IResponse> SendAsync(this IDocumentLoader loader, DocumentRequest request, CancellationToken cancel)
 {
     return loader != null ? loader.LoadAsync(request, cancel) : TaskEx.FromResult(default(IResponse));
 }
        async Task Receive(IDocumentLoader loader, DocumentRequest request, CancellationToken cancel)
        {
            try
            {
                using (var response = await loader.DownloadAsync(request).Task.ConfigureAwait(false))
                {
                    if (response != null)
                    {
                        foreach (var header in response.Headers)
                            _headers[header.Key] = header.Value;

                        _responseUrl = response.Address.Href;
                        _responseStatus = response.StatusCode;
                        ReadyState = RequesterState.Loading;

                        using (var ms = new MemoryStream())
                        {
                            await response.Content.CopyToAsync(ms, BufferSize, cancel).ConfigureAwait(false);
                            ms.Seek(0, SeekOrigin.Begin);

                            using (var reader = new StreamReader(ms))
                                _responseText = reader.ReadToEnd();
                        }

                        Fire(LoadEndEvent);
                    }

                    ReadyState = RequesterState.Done;
                    Fire(response == null ? ErrorEvent : LoadEvent);
                }
            }
            catch (TaskCanceledException)
            {
                ReadyState = RequesterState.Done;
                Fire(TimeoutEvent);
            }
        }
        public void Send(Object body = null)
        {
            if (_readyState != RequesterState.Opened)
                return;

            var requestBody = Serialize(body);
            var mimeType = default(String);
            var loader = GetLoader();

            if (loader != null)
            {
                var request = new DocumentRequest(_url)
                {
                    Body = requestBody,
                    Method = _method,
                    MimeType = mimeType,
                    Referer = _window.Document.DocumentUri,
                };

                foreach (var header in _headers)
                    request.Headers[header.Key] = header.Value;

                _headers.Clear();
                _cancel.CancelAfter(_timeout);

                Fire(LoadStartEvent);
                ReadyState = RequesterState.HeadersReceived;
                var connection = Receive(loader, request, _cancel.Token);

                if (!_async)
                    connection.Wait();
            }
        }
Exemplo n.º 9
0
 /// <summary>
 /// Plan to navigate to an action using the specified method with the given
 /// entity body of the mime type.
 /// http://www.w3.org/html/wg/drafts/html/master/forms.html#plan-to-navigate
 /// </summary>
 /// <param name="element">The element to navigate from.</param>
 /// <param name="request">The request to issue.</param>
 /// <returns>A task that will eventually result in a new document.</returns>
 public static async Task<IDocument> NavigateToAsync(this Element element, DocumentRequest request)
 {
     var download = element.Owner.Context.Loader.DownloadAsync(request);
     var response = await download.Task.ConfigureAwait(false);
     var cancel = CancellationToken.None;
     return await element.Owner.Context.OpenAsync(response, cancel).ConfigureAwait(false);
 }
Exemplo n.º 10
0
 /// <summary>
 /// Plan to navigate to an action using the specified method with the given
 /// entity body of the mime type.
 /// http://www.w3.org/html/wg/drafts/html/master/forms.html#plan-to-navigate
 /// </summary>
 /// <param name="request">The request to issue.</param>
 Task<IDocument> NavigateTo(DocumentRequest request)
 {
     this.CancelTasks();
     return this.CreateTask(cancel => Owner.Context.OpenAsync(request, cancel));
 }
Exemplo n.º 11
0
 /// <summary>
 /// Plan to navigate to an action using the specified method with the given
 /// entity body of the mime type.
 /// http://www.w3.org/html/wg/drafts/html/master/forms.html#plan-to-navigate
 /// </summary>
 /// <param name="request">The request to issue.</param>
 Task<IDocument> NavigateTo(DocumentRequest request)
 {
     var owner = Owner;
     owner.Tasks.Cancel(_current);
     return owner.Tasks.Add(cancel => Owner.Context.OpenAsync(request, cancel));
 }