Click here to Skip to main content
15,892,059 members
Articles / Programming Languages / C#

Harvesting Web Content into MHTML Archive

Rate me:
Please Sign up or sign in to vote.
4.69/5 (14 votes)
18 Feb 20068 min read 62K   729   49  
Capture and archive web resources by saving them in RFC-2557 (MHTML) compliant format. This library includes a framework for augmenting the capture process and allowing programmatic control, from downloading through saving. A replacement for CDOSYS/CDONTS.
using System;
using Rilling.MhtmlLib.Media.Artifacts;
using System.Text;
using System.Net;
using System.Xml;

namespace Rilling.MhtmlLib.Media.Handlers
{
	/// <summary>
	///		A factory that creates an <see cref="HtmlArtifact"/> object.
	/// </summary>
	public class HtmlContentHandler : TextContentHandler
	{
		/// <summary>
		///		Initializes a new <see cref="HtmlArtifact"/>
		///		instance with information from the specified 
		///		<see cref="HttpWebResponse"/> object.
		/// </summary>
		/// <param name="webResponse">
		///		The <see cref="HttpWebResponse"/> used to initialize this instance.
		/// </param>
		/// <returns>
		///		An initialized <see cref="HtmlArtifact"/> instance.
		/// </returns>
		/// <remarks>
		///		Currently there is no validation to ensure the content in the
		///		<see cref="HttpWebResponse"/> is compatible with this artifact.
		/// </remarks>
		public override ArtifactBase
			CreateArtifact(HttpWebResponse webResponse)
		{
			if(webResponse == null) 
				throw(new ArgumentNullException("webResponse"));

			HtmlArtifact artifact = 
				new HtmlArtifact(webResponse.GetResponseStream());

			//
			// Initialize all necessary properties from the web content.
			//
			artifact.ContentType	= webResponse.ContentType.Split(';')[0];
			artifact.Location		= webResponse.ResponseUri;

			string charCode = webResponse.CharacterSet;
			if (charCode != String.Empty)
				artifact.Encoding = Encoding.GetEncoding(charCode);

			return artifact;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions