或如何在新建ChromiumWebBrowser实例时将自定义标头注入到网站的初始请求中。
我是Chromium的菜鸟,可以真正使用一些帮助。我有一个带CEF窗口的winforms应用程序。 K,到目前为止没有问题。我需要做的是使用包含身份验证信息的自定义http-header调用/加载初始URL。这可能吗?
以下基本上是正在发挥作用的所有部分除了自定义标题(Doh!)
之外还有效Winform(CEF httpRequest(带有自定义标题))[永远不会超过这一点] => C#MVC web app => Owin_Authentication_Pipeline segment =>具有填充的Razor视图的MVC响应=>显示在Winform Chromium应用程序中。
也许这也会有所帮助:
using CefSharp;
using CefSharp.WinForms;
...
private void Form1_Load(object sender, EventArgs e)
{
Cef.Initialize();
ChromiumWebBrowser myBrowser = new ChromiumWebBrowser("whatever.com");
// ??How do i get a custom header be sent with the above line??
myBrowser.Dock = DockStyle.Fill;
//myBrowser.ShowDevTools();
//myBrowser.RequestHandler = new DSRequestHander();
//myBrowser.FrameLoadStart += myBrowser_FrameLoadStart;
this.Controls.Add(myBrowser);
}
我把它弄死了,看了看,尝试了我工具箱中的所有技巧,然后是一些。
非常感谢任何有关我如何能够解决或绕过这个boggler的想法,帮助或提示。提前谢谢。
答案 0 :(得分:7)
实施并指派IRequestHandler
。你的方法应该是OnBeforeResourceLoad
,一个基本的例子应该是
bool IRequestHandler.OnBeforeResourceLoad(IWebBrowser browser, IRequest request, IResponse response)
{
var headers = request.Headers;
headers["Custom-Header"] = "My Custom Header";
request.Headers = headers;
return false;
}
目前,您必须阅读headers属性,进行更改然后重新分配。
答案 1 :(得分:5)
您应该创建一个实现IRequestHandler
的类,然后在浏览器对象中将该类的实例设置为RequestHandler
。
对于版本53,该类应如下所示:
class ChromeBrowserRequestHandler: IRequestHandler
{
public bool GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback)
{
return false;
}
public bool OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, bool isRedirect)
{
return false;
}
public bool OnBeforePluginLoad(IWebBrowser browser, string url, string policyUrl, WebPluginInfo info)
{
return false;
}
public CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
var headers = request.Headers;
headers["Custom-Header"] = "My Custom Header";
request.Headers = headers;
return CefReturnValue.Continue;
}
public bool OnCertificateError(IWebBrowser browser, CefErrorCode errorCode, string requestUrl)
{
return false;
}
public void OnPluginCrashed(IWebBrowser browser, string pluginPath)
{
}
public void OnRenderProcessTerminated(IWebBrowser browserControl, IBrowser browser, CefTerminationStatus status)
{
}
public IResponseFilter GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
return null;
}
public bool OnCertificateError(IWebBrowser browserControl, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback)
{
return false;
}
public bool OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture)
{
return false;
}
public void OnPluginCrashed(IWebBrowser browserControl, IBrowser browser, string pluginPath)
{
}
public bool OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url)
{
return false;
}
public bool OnQuotaRequest(IWebBrowser browserControl, IBrowser browser, string originUrl, long newSize, IRequestCallback callback)
{
return false;
}
public void OnRenderViewReady(IWebBrowser browserControl, IBrowser browser)
{
}
public void OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
{
}
public void OnResourceRedirect(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, ref string newUrl)
{
}
public bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
return false;
}
}
然后,在创建浏览器对象时:
ChromiumWebBrowser myBrowser = new ChromiumWebBrowser("whatever.com")
{
RequestHandler = new ChromeBrowserRequestHandler()
};
请注意,必须在加载页面之前设置请求处理程序。如果您无法在instanctiaction期间设置请求处理程序,您仍可以稍后设置它,并使用myBrowser.Load("whatever.com")
重新加载页面。
答案 2 :(得分:1)
在最新版本之一中,一些回调已从IRequestHandler
移至IResourceRequestHandler
接口。最简单的方法是覆盖默认实现RequestHandler
和ResourceRequestHandler
,例如:
class BearerAuthResourceRequestHandler : ResourceRequestHandler
{
public BearerAuthResourceRequestHandler(string token)
{
_token = token;
}
private string _token;
protected override CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
{
if (!string.IsNullOrEmpty(_token))
{
var headers = request.Headers;
headers["Authorization"] = $"Bearer {_token}";
request.Headers = headers;
return CefReturnValue.Continue;
}
else return base.OnBeforeResourceLoad(chromiumWebBrowser, browser, frame, request, callback);
}
}
class BearerAuthRequestHandler : RequestHandler
{
public BearerAuthRequestHandler(string token)
{
_token = token;
}
private string _token;
protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
{
if (!string.IsNullOrEmpty(_token)) return new BearerAuthResourceRequestHandler(_token);
else return base.GetResourceRequestHandler(chromiumWebBrowser, browser, frame, request, isNavigation, isDownload, requestInitiator, ref disableDefaultHandling);
}
}
然后,将其分配给浏览器RequestHandler
:
Browser.RequestHandler = new BearerAuthRequestHandler(token);