Asp.net中使用mshtml

在asp.net中使用mshtml  Com组件

需要添加接口IPersistStreamInit

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
 
namespace MsHtmlWeb
{
    [ComVisible(true), ComImport(),
Guid("7FD52380-4E07-101B-AE2D-08002B2EC713"),
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IPersistStreamInit
    {
        void GetClassID([In, Out] ref Guid pClassID);
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int IsDirty();
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int Load([In] UCOMIStream pstm);
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int Save([In] UCOMIStream pstm, [In,
            MarshalAs(UnmanagedType.Bool)] bool fClearDirty);
        void GetSizeMax([Out] long pcbSize);
        [return: MarshalAs(UnmanagedType.I4)]
        [PreserveSig]
        int InitNew();
    }
 
}
 
 

在asp.net页面中

namespace MsHtmlWeb
{
    public partial class WebFormMSHtml : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
 
            }
            //mshtml.HTMLDocument objMSHTML = new mshtml.HTMLDocument();
            //mshtml.IHTMLDocument2 objMSHTML2;
            //mshtml.IHTMLDocument3 objMSHTML3;
            //int x = 10;
            ////a dummy variable
            ////IPersistStreamInit objIPS;
            //////here is the whole trick
            ////objIPS = (IPersistStreamInit)objMSHTML;
            //objIPS.InitNew();
            ////you have to do it, if not you will always have readyState as "loading"
            //objMSHTML2 = objMSHTML.createDocumentFromUrl(url, null);
            //while (!(objMSHTML2.readyState == "complete"))
            //{
            //    x = x + 1;
            //}
            //objMSHTML3 = (mshtml.IHTMLDocument3)objMSHTML2;
            //IHTMLElementCollection d = objMSHTML3.getElementsByName("q");
            //IHTMLElementCollection de = objMSHTML3.getElementsByName("btnG");
            //HTMLInputElementClass cn = (HTMLInputElementClass)d.item("q", 0);
            //cn.value = "biso";
            //HTMLInputElementClass bt = (HTMLInputElementClass)de.item("btnG", 0);
            //bt.click();
            //// this does not fire
            //string s = objMSHTML3.documentElement.innerHTML;
        }
 
        protected void Button1_Click(object sender, EventArgs e)
        {
            mshtml.HTMLDocument  htmldoc;
            //IHTMLDocument2 htmldoc;
            htmldoc = new mshtml.HTMLDocument();
            //htmldoc = new IHTMLDocument2();
            mshtml.IHTMLDocument2 htmldoc2;
            mshtml.IHTMLDocument3 htmldoc3;
 
            IPersistStreamInit ips = (IPersistStreamInit)htmldoc;
            ips.InitNew();
            htmldoc2 = (mshtml.IHTMLDocument2)htmldoc.createDocumentFromUrl("http://www.baidu.com", null);
            while (htmldoc2.readyState != "complete") ;
            htmldoc3 = (mshtml.IHTMLDocument3)htmldoc2;
 
            //Literal ltr = new Literal();
            //ltr.Text = Server.HtmlEncode(htmldoc3.documentElement.innerHTML);
            //Panel1.Controls.Add(ltr);
            IHTMLElement he = htmldoc3.getElementById("kw");
            he.setAttribute("value", "F555");
            string s = htmldoc3.documentElement.innerHTML;
 
            Label1.Text = s;//he.tagName+"z";
        }
        private void aa(string strString)
        { 
            mshtml.HTMLDocument htmldocument=new HTMLDocument();
            mshtml.IHTMLDocument2 doc = htmldocument.createDocumentFromUrl(strString, "");
                //,mshtml.IHTMLDocument2
            IHTMLElement2 element = doc.body as IHTMLElement2;
            //string s=element.
        //Dim htmldocument as New mshtml.htmldocument
             
//Dim doc As mshtml.IHTMLDocument2 =
//CType(htmldocument.createDocumentFromUrl(strString, ""),
//mshtml.IHTMLDocument2)
//Dim timeout As DateTime = Now
//Do Until htmldocument.readyState = "complete" Or Now.Subtract
//(timeout).TotalSeconds >= 4
//System.Threading.Thread.CurrentThread.Sleep(100)
//Loop
        }
 
        protected void Button2_Click(object sender, EventArgs e)
        {
            mshtml.HTMLDocument htmldoc;
            //IHTMLDocument2 htmldoc;
            htmldoc = new mshtml.HTMLDocument();
            //htmldoc = new IHTMLDocument2();
            mshtml.IHTMLDocument2 htmldoc2;
            mshtml.IHTMLDocument3 htmldoc3;
 
            IPersistStreamInit ips = (IPersistStreamInit)htmldoc;
            ips.InitNew();
            htmldoc2 = (mshtml.IHTMLDocument2)htmldoc.createDocumentFromUrl("http://baidu.lehecai.com/lottery/draw/view/200", null);
            while (htmldoc2.readyState != "complete") ;
            htmldoc3 = (mshtml.IHTMLDocument3)htmldoc2;
 
            IHTMLElement he = htmldoc3.getElementById("jq_latest_draw_result");
            string strLast = string.Empty;
            if (!string.IsNullOrEmpty(he.innerText))
            {
                  strLast = he.innerText.Trim();
            }
            if (!string.IsNullOrEmpty(strLast))
            {
                string[] strList = strLast.Split(new char[] { ' ' });
                string aa = string.Empty;
                foreach (var item in strList)
                {
                    aa += item + ",";
                }
                if (aa.EndsWith(","))
                    aa = aa.TrimEnd(new char[] { ',' });
                Label2.Text = aa;
            }
           // 
        }
 
    }
}
 

发生错误解决

在vs2010中运行HTMLDocumentClass doc =new HTMLDocumentClass(); 会出现Interop type 'mshtml.HTMLDocumentClass' cannot be embedded. Use the applicable interface instead.的编译错误。原因是我们添加 Microsoft.mshtml的时候,由于mshtml.dll太大,微软设置了这个属性Embed Interop Type =true .

解决的办法是:直接去掉Class后缀。

原文地址:https://www.cnblogs.com/z_lb/p/2217910.html