我需要使用System.Windows.Controls.WebBrowser控件向我传输的每个请求发送标头。使用C#和.Net,由于描述here的错误,BeforeNavigate2事件(可以帮助我调用引用参数)不会被触发,并且错误报告中描述的BeforeNavigate事件不会帮助我,因为它的参数是只读的。
所描述的解决方案here难以使用,因为我有很多对控件Web浏览器的引用,这些引用必须通过此项目中的现有接口和其他库来解决。
与所描述的解决方案一样,只需要一个我可以在外部设置的固定字符串即可,但我需要使用Controls Web Browser设置Headers的解决方案。是否有一个解决方案可以满足我的要求而不使用表单?
答案 0 :(得分:4)
这是另一种方法,它使用低级COM连接点容器接口直接连接到底层DWebBrowserEvents2
源接口。
从here获取完整的项目源代码。我没有VS2010,所以它是一个VS2012项目,但它的目标是.NET 4.0。如果您准备按原样尝试,则已编译的应用程序包含在Debug
文件夹中。
这是相关代码。这是一个概念的快速证明,它仍然可能存在错误:
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows;
namespace WpfWebBrowserEvents
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += (s, e) =>
{
var sink = new WebBrowserEventSink();
sink.Connect(this.webBrowser);
this.webBrowser.Navigate("http://example.com");
};
}
}
/// <summary>
/// Handling WebBrowser ActiveX events directly
/// by Noseratio - http://stackoverflow.com/q/20838264/1768303
/// </summary>
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[ComDefaultInterface(typeof(SHDocVw.DWebBrowserEvents2))]
public class WebBrowserEventSink : SHDocVw.DWebBrowserEvents2
{
System.Runtime.InteropServices.ComTypes.IConnectionPoint _sinkCP = null;
int _sinkCookie = int.MaxValue;
public void Connect(System.Windows.Controls.WebBrowser webBrowser)
{
if (_sinkCookie != int.MaxValue)
throw new InvalidOperationException();
var activeXInstance = webBrowser.GetType().InvokeMember("ActiveXInstance",
BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic,
null, webBrowser, new object[] { }) as SHDocVw.WebBrowser;
var cpc = (System.Runtime.InteropServices.ComTypes.IConnectionPointContainer)activeXInstance;
var guid = typeof(SHDocVw.DWebBrowserEvents2).GUID;
System.Runtime.InteropServices.ComTypes.IConnectionPoint _sinkCP;
cpc.FindConnectionPoint(ref guid, out _sinkCP);
_sinkCP.Advise(this, out _sinkCookie);
}
public void Disconnect()
{
if (_sinkCookie == int.MaxValue)
throw new InvalidOperationException();
_sinkCP.Unadvise(_sinkCookie);
_sinkCookie = int.MaxValue;
_sinkCP = null;
}
#region SHDocVw.DWebBrowserEvents2
public void StatusTextChange(string Text)
{
}
public void ProgressChange(int Progress, int ProgressMax)
{
}
public void CommandStateChange(int Command, bool Enable)
{
}
public void DownloadBegin()
{
}
public void DownloadComplete()
{
}
public void TitleChange(string Text)
{
}
public void PropertyChange(string szProperty)
{
}
public void BeforeNavigate2(object pDisp, ref object URL, ref object Flags, ref object TargetFrameName, ref object PostData, ref object Headers, ref bool Cancel)
{
MessageBox.Show("BeforeNavigate2: " + URL.ToString());
}
public void NewWindow2(ref object ppDisp, ref bool Cancel)
{
}
public void NavigateComplete2(object pDisp, ref object URL)
{
}
public void DocumentComplete(object pDisp, ref object URL)
{
}
public void OnQuit()
{
}
public void OnVisible(bool Visible)
{
}
public void OnToolBar(bool ToolBar)
{
}
public void OnMenuBar(bool MenuBar)
{
}
public void OnStatusBar(bool StatusBar)
{
}
public void OnFullScreen(bool FullScreen)
{
}
public void OnTheaterMode(bool TheaterMode)
{
}
public void WindowSetResizable(bool Resizable)
{
}
public void WindowSetLeft(int Left)
{
}
public void WindowSetTop(int Top)
{
}
public void WindowSetWidth(int Width)
{
}
public void WindowSetHeight(int Height)
{
}
public void WindowClosing(bool IsChildWindow, ref bool Cancel)
{
}
public void ClientToHostWindow(ref int CX, ref int CY)
{
}
public void SetSecureLockIcon(int SecureLockIcon)
{
}
public void FileDownload(bool ActiveDocument, ref bool Cancel)
{
}
public void NavigateError(object pDisp, ref object URL, ref object Frame, ref object StatusCode, ref bool Cancel)
{
}
public void PrintTemplateInstantiation(object pDisp)
{
}
public void PrintTemplateTeardown(object pDisp)
{
}
public void UpdatePageStatus(object pDisp, ref object nPage, ref object fDone)
{
}
public void PrivacyImpactedStateChange(bool bImpacted)
{
}
public void NewWindow3(ref object ppDisp, ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl)
{
}
public void SetPhishingFilterStatus(int PhishingFilterStatus)
{
}
public void WindowStateChanged(uint dwWindowStateFlags, uint dwValidFlagsMask)
{
}
public void NewProcess(int lCauseFlag, object pWB2, ref bool Cancel)
{
}
public void ThirdPartyUrlBlocked(ref object URL, uint dwCount)
{
}
public void RedirectXDomainBlocked(object pDisp, ref object StartURL, ref object RedirectURL, ref object Frame, ref object StatusCode)
{
}
public void BeforeScriptExecute(object pDispWindow)
{
}
public void WebWorkerStarted(uint dwUniqueID, string bstrWorkerLabel)
{
}
public void WebWorkerFinsihed(uint dwUniqueID)
{
}
#endregion
}
}