public string Capture(int timeoutMs) { // flag indicating whether we should continue with the capture bool continueCapture = true; // request the page HttpWebResponse response = RequestPage(TargetUrl, timeoutMs); OnHeadersReceived(response.Headers, ref continueCapture); if (!continueCapture) { throw new OperationCancelledException(); } // transfer it to a stream MemoryStream pageStream = new MemoryStream(); using (Stream responseStream = response.GetResponseStream()) StreamHelper.Transfer(responseStream, pageStream); pageStream.Seek(0, SeekOrigin.Begin); // allow filter on content OnContentReceived(new StreamReader(pageStream).ReadToEnd(), ref continueCapture); if (!continueCapture) { throw new OperationCancelledException(); } pageStream.Seek(0, SeekOrigin.Begin); // Read the stream into a lightweight HTML doc. We use from LightWeightHTMLDocument.FromIHTMLDocument2 // instead of LightWeightHTMLDocument.FromStream because from stream improperly shoves a saveFrom declaration // above the docType (bug 289357) IHTMLDocument2 doc = HTMLDocumentHelper.StreamToHTMLDoc(pageStream, TargetUrl, false); LightWeightHTMLDocument ldoc = LightWeightHTMLDocument.FromIHTMLDocument2(doc, TargetUrl, true); // download references FileBasedSiteStorage siteStorage = new FileBasedSiteStorage(DestinationPath, "index.htm"); PageToDownload page = new PageToDownload(ldoc, TargetUrl, siteStorage.RootFile); PageAndReferenceDownloader downloader = new PageAndReferenceDownloader(new PageToDownload[] { page }, siteStorage); downloader.Download(new TimeoutProgressHost(timeoutMs)); // return path to captured page return(Path.Combine(DestinationPath, siteStorage.RootFile)); }
public string Capture(int timeoutMs) { // flag indicating whether we should continue with the capture bool continueCapture = true ; // request the page HttpWebResponse response = RequestPage(TargetUrl, timeoutMs); OnHeadersReceived(response.Headers, ref continueCapture) ; if ( !continueCapture ) throw new OperationCancelledException() ; // transfer it to a stream MemoryStream pageStream = new MemoryStream(); using ( Stream responseStream = response.GetResponseStream() ) StreamHelper.Transfer(responseStream, pageStream); pageStream.Seek(0, SeekOrigin.Begin) ; // allow filter on content OnContentReceived( new StreamReader(pageStream).ReadToEnd(), ref continueCapture ) ; if ( !continueCapture ) throw new OperationCancelledException() ; pageStream.Seek(0, SeekOrigin.Begin) ; // Read the stream into a lightweight HTML doc. We use from LightWeightHTMLDocument.FromIHTMLDocument2 // instead of LightWeightHTMLDocument.FromStream because from stream improperly shoves a saveFrom declaration // above the docType (bug 289357) IHTMLDocument2 doc = HTMLDocumentHelper.StreamToHTMLDoc(pageStream, TargetUrl, false); LightWeightHTMLDocument ldoc = LightWeightHTMLDocument.FromIHTMLDocument2(doc, TargetUrl, true); // download references FileBasedSiteStorage siteStorage = new FileBasedSiteStorage(DestinationPath, "index.htm"); PageToDownload page = new PageToDownload(ldoc, TargetUrl, siteStorage.RootFile); PageAndReferenceDownloader downloader = new PageAndReferenceDownloader(new PageToDownload[]{page}, siteStorage) ; downloader.Download(new TimeoutProgressHost(timeoutMs)) ; // return path to captured page return Path.Combine(DestinationPath, siteStorage.RootFile) ; }
private string DownloadTemplateFiles(string templateContents, string templateUrl, IProgressHost progress) { progress.UpdateProgress(Res.Get(StringId.ProgressDownloadingSupportingFiles)); FileBasedSiteStorage files = new FileBasedSiteStorage(_blogTemplateDir); // convert the string to a stream MemoryStream templateStream = new MemoryStream(); StreamWriter writer = new StreamWriter(templateStream, Encoding.UTF8); writer.Write(templateContents); writer.Flush(); templateStream.Seek(0, SeekOrigin.Begin); //read the stream into a lightweight HTML. Note that we use from LightWeightHTMLDocument.FromIHTMLDocument2 //instead of LightWeightHTMLDocument.FromStream because from stream improperly shoves a saveFrom declaration //above the docType (bug 289357) IHTMLDocument2 doc = HTMLDocumentHelper.StreamToHTMLDoc(templateStream, templateUrl, true); LightWeightHTMLDocument ldoc = LightWeightHTMLDocument.FromIHTMLDocument2(doc, templateUrl, true, false); PageDownloadContext downloadContext = new PageDownloadContext(0); ApplyCredentials(downloadContext, templateUrl); using (PageToDownloadFactory downloadFactory = new PageToDownloadFactory(ldoc, downloadContext, _parentControl)) { //calculate the dependent styles and resources ProgressTick tick = new ProgressTick(progress, 50, 100); downloadFactory.CreatePagesToDownload(tick); tick.UpdateProgress(100, 100); //download the dependent styles and resources tick = new ProgressTick(progress, 50, 100); PageAndReferenceDownloader downloader = new PageAndReferenceDownloader(downloadFactory.PagesToDownload, files); this.ApplyCredentials(downloader, templateUrl); downloader.Download(tick); tick.UpdateProgress(100, 100); //Expand out the relative paths in the downloaded HTML file with absolute paths. //Note: this is necessary so that template resources are not improperly resolved relative // to the location of the file the editor is editing. string blogTemplateFile = Path.Combine(_blogTemplateDir, files.RootFile); string origFile = blogTemplateFile + ".token"; File.Move(blogTemplateFile, origFile); string absPath = String.Format(CultureInfo.InvariantCulture, "file:///{0}/{1}", _blogTemplateDir.Replace('\\', '/'), downloader.PathToken); TextHelper.ReplaceInFile(origFile, downloader.PathToken, blogTemplateFile, absPath); File.Delete(origFile); //fix up the files FixupDownloadedFiles(blogTemplateFile, files, downloader.PathToken); //complete the progress. progress.UpdateProgress(100, 100); File.WriteAllText(blogTemplateFile + ".path", absPath); return blogTemplateFile; } }
private void ApplyCredentials(PageAndReferenceDownloader downloader, string url) { WinInetCredentialsContext credentialsContext = CreateCredentialsContext(url); if (credentialsContext != null) downloader.CredentialsContext = credentialsContext; }