使用IPersistStreamInit从WPF-WebBrowser-Control获取HTML源

本文关键字:HTML 获取 WPF-WebBrowser-Control IPersistStreamInit 使用 | 更新日期: 2023-09-27 18:09:16

我正试图获得已加载到WPF WebBrowser控件的网页的HTML源。要做到这一点的唯一方法似乎是强制转换WebBrowser的实例。文档到IPersistStreamInit(我必须自己定义,因为它是一个COM接口)并调用IPersistStreamInit。保存方法,传递一个实现一个IStream(同样是一个COM接口),它将持久化文档到流。嗯,有点:我总是得到流的前4kb,而不是整个文档,我不知道为什么。

以下是IPersistStreamInit的代码:
using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Security;
namespace PayPal.SkyNet.BpiTool.Interop
{
    [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), 
        SuppressUnmanagedCodeSecurity, 
        Guid("7FD52380-4E07-101B-AE2D-08002B2EC713")]
    public interface IPersistStreamInit
    {
        void GetClassID(out Guid pClassID);
        [PreserveSig]
        int IsDirty();
        void Load([In, MarshalAs(UnmanagedType.Interface)] IStream pstm);
        void Save([In, MarshalAs(UnmanagedType.Interface)] IStream pstm, [In, MarshalAs(UnmanagedType.Bool)] bool fClearDirty);
        void GetSizeMax([Out, MarshalAs(UnmanagedType.LPArray)] long pcbSize);
        void InitNew();
    }
}
下面是IStream-Implementation的代码:
using System;
using System.IO;
using System.Runtime.InteropServices.ComTypes;
namespace PayPal.SkyNet.BpiTool.Interop
{
    public class ComStream : IStream
    {
        private Stream _stream;
        public ComStream(Stream stream)
        {
            this._stream = stream;
        }
        public void Commit(int grfCommitFlags)
        {
        }
        public void CopyTo(IStream pstm, long cb, IntPtr pcbRead, IntPtr pcbWritten)
        {
        }
        public void LockRegion(long libOffset, long cb, int dwLockType)
        {
        }
        public void Read(byte[] pv, int cb, IntPtr pcbRead)
        {
            this._stream.Read(pv, (int)this._stream.Position, cb);
        }
        public void Revert()
        {
        }
        public void SetSize(long libNewSize)
        {
            this._stream.SetLength(libNewSize);
        }
        public void Stat(out System.Runtime.InteropServices.ComTypes.STATSTG pstatstg, int grfStatFlag)
        {
            pstatstg = new System.Runtime.InteropServices.ComTypes.STATSTG();
        }
        public void UnlockRegion(long libOffset, long cb, int dwLockType)
        {
        }
        public void Write(byte[] pv, int cb, IntPtr pcbWritten)
        {
            this._stream.Write(pv, 0, cb);
        }
        public void Clone(out IStream outputStream)
        {
            outputStream = null;
        }
        public void Seek(long dlibMove, int dwOrigin, IntPtr plibNewPosition)
        {
            this._stream.Seek(dlibMove, (SeekOrigin)dwOrigin);
        }
    }
}

现在我有一个类来总结这一切。由于我不想重新分发mshtml-interop-assembly,所以我选择了延迟绑定,并且由于在VB中延迟绑定更容易,所以我在VB中进行了绑定。下面是代码:

Option Strict Off
Option Explicit Off
Imports System.IO
Public Class HtmlDocumentWrapper : Implements IDisposable
    Private htmlDoc As Object
    Public Sub New(ByVal htmlDoc As Object)
        Me.htmlDoc = htmlDoc
    End Sub
    Public Property Document As Object
        Get
            Return Me.htmlDoc
        End Get
        Set(value As Object)
            Me.htmlDoc = Nothing
            Me.htmlDoc = value
        End Set
    End Property
    Public ReadOnly Property DocumentStream As Stream
        Get
            Dim str As Stream = Nothing
            Dim psi As IPersistStreamInit = CType(Me.htmlDoc, IPersistStreamInit)
            If psi IsNot Nothing Then
                str = New MemoryStream
                Dim cStream As New ComStream(str)
                psi.Save(cStream, False)
                str.Position = 0
            End If
            Return str
        End Get
    End Property
End Class

现在我应该可以使用所有这些了:

private void Browser_Navigated(object sender, NavigationEventArgs e)
{
    HtmlDocumentWrapper doc = new HtmlDocumentWrapper();
    doc.Document = Browser.Document;
    using (StreamReader sr = new StreamReader(doc.DocumentStream))
    {
        using (StreamWriter sw = new StreamWriter("test.txt"))
        {
            //BOOM! Only 4kb of HTML source
            sw.WriteLine(sr.ReadToEnd());
            sw.Flush();
        }
    }
}

有谁知道,为什么我没有得到整个HTML源代码?如有任何帮助,不胜感激。

Arne

使用IPersistStreamInit从WPF-WebBrowser-Control获取HTML源

从浏览器中移动代码。导航到浏览器。LoadCompleted如Sheng Jiang正确指出的那样,它工作

这只是一个猜测:

流没有已知的长度,因为它可能仍在下载中。您需要阅读它,直到它显示EOF。