下载网页后保存为mht格式的文件

原文 http://www.eggheadcafe.com/articles/20040527.asp

引入com组件
Microsoft Activex Data Object 2.x library

Microsoft CDO For Exchange 2000 libarary
具体程序如下.
using System;

using System.Web;

using CDO;

using ADODB;

using System.Text;

namespace PAB.Web.Utils

{

public class MIMEConverter

{

//private ctor as our methods are all static here

private MIMEConverter()

{

}

public static bool SaveWebPageToMHTFile( string url, string filePath)

{

bool result=false;

CDO.Message  msg = new CDO.MessageClass();

ADODB.Stream  stm=null ;

try

{

msg.MimeFormatted =true;

msg.CreateMHTMLBody(url,CDO.CdoMHTMLFlags.cdoSuppressNone, "" ,"" );
stm = msg.GetStream(); stm.SaveToFile(filePath,ADODB.SaveOptionsEnum.adSaveCreateOverWrite); msg=null; stm.Close(); result=true; } catch {throw;} finally { //cleanup here } return result; } public static string ConvertWebPageToMHTString( string url ) { string data = String.Empty; CDO.Message msg = new CDO.MessageClass(); ADODB.Stream stm=null; try { msg.MimeFormatted =true; msg.CreateMHTMLBody(url,CDO.CdoMHTMLFlags.cdoSuppressNone,
"", "" );
stm = msg.GetStream(); data= stm.ReadText(stm.Size); } catch { throw; } finally { //cleanup here } return data; } } }

你可能感兴趣的:(文件)