/// <summary> /// Opens a new document created from the response asynchronously in /// the given context. /// </summary> /// <param name="context">The browsing context to use.</param> /// <param name="response">The response to examine.</param> /// <param name="cancel">The cancellation token.</param> /// <returns>The task that creates the document.</returns> public static Task <IDocument> OpenAsync(this IBrowsingContext context, IResponse response, CancellationToken cancel) { if (response == null) { throw new ArgumentNullException("response"); } if (context == null) { context = BrowsingContext.New(); } var contentType = response.GetContentType(MimeTypes.Html); var encoding = context.Configuration.DefaultEncoding(); var charset = contentType.GetParameter(AttributeNames.Charset); if (!String.IsNullOrEmpty(charset) && TextEncoding.IsSupported(charset)) { encoding = TextEncoding.Resolve(charset); } var source = new TextSource(response.Content, encoding); return(context.LoadDocumentAsync(response, contentType, source, cancel)); }
/// <summary> /// Opens a new document created from the response asynchronously in /// the given context. /// </summary> /// <param name="context">The browsing context to use.</param> /// <param name="response">The response to examine.</param> /// <param name="cancel">The cancellation token.</param> /// <returns>The task that creates the document.</returns> public static Task <IDocument> OpenAsync(this IBrowsingContext context, IResponse response, CancellationToken cancel) { if (response == null) { throw new ArgumentNullException("response"); } if (context == null) { context = BrowsingContext.New(); } var source = new TextSource(response.Content, context.Configuration.DefaultEncoding()); return(context.LoadDocumentAsync(response, source, cancel)); }
/// <summary> /// Opens a new document loaded from a virtual response that can be /// filled via the provided callback. /// </summary> /// <param name="context">The browsing context to use.</param> /// <param name="request">Callback with the response to setup.</param> /// <param name="cancel">The cancellation token.</param> /// <returns>The task that creates the document.</returns> public static Task <IDocument> OpenAsync(this IBrowsingContext context, Action <VirtualResponse> request, CancellationToken cancel) { if (request == null) { throw new ArgumentNullException("request"); } if (context == null) { context = BrowsingContext.New(); } using (var response = new VirtualResponse()) { request(response); var source = response.CreateSourceFor(context.Configuration); return(context.LoadDocumentAsync(response, source, cancel)); } }