铬-在初始页面加载时发送自定义标头信息c# [英] chromium - send custom header info on initial page load c#

查看:167
本文介绍了铬-在初始页面加载时发送自定义标头信息c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在新建ChromiumWebBrowser实例时,如何向网站的初始请求中注入自定义标头。

我是Chromium的新手,真的可以使用一些帮助。我有一个带有CEF窗口的Winforms应用程序。 K,到目前为止没有问题。我需要做的是使用包含身份验证信息的自定义HTTP标头调用/加载初始网址。这可能吗?

I'm a noob with Chromium and could really use some help. I have a winforms app with a CEF window. K, no prob so far. What I need to do is to call/load the initial url with a custom http-header that contains authentication info. Is this possible?

以下本质上是起作用的,除了自定义标题(Doh!)以外,所有其他部分都起作用。

The following is essentially what is at play and all parts work except the custom header (Doh!)

Winform(CEF httpRequest(带有自定义标头))[永远不要超过此点] => C#MVC Web应用=> Owin_Authentication_Pipeline段=>具有填充的Razor视图的MVC响应=>在Winform Chromium应用中显示。

Winform(CEF httpRequest(with custom header)) [never gets past this point]=> C# MVC web app => Owin_Authentication_Pipeline segment => MVC Response with populated Razor view => Shows up in Winform Chromium app.

也许这也会有所帮助:

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);
}

我把这个笑到了死,看着,尝试了我工具箱中的所有技巧,

I Groggled this to death, looked, tried all the tricks in my toolbox and then some.

任何关于我如何能够解决或解决这个难题的想法,帮助或提示,将不胜感激。

Any ideas, help or hints on how I might be able to solve or get around this boggler is greatly appreciated. Thanks in advance.

推荐答案

已更新,以反映铬的重大更改

已更新,以反映版本 75 中所做的更改(应在 75 及更高版本中使用)

Updated to reflect changes made in version 75 (should work in 75 and newer)

您追求的方法应该是 OnBeforeResourceLoad ,一个基本示例应如下所示:

The method you're after should be OnBeforeResourceLoad, a basic example should look like:

public class CustomResourceRequestHandler : ResourceRequestHandler
{
    protected override CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
    {
        var headers = request.Headers;
        headers["User-Agent"] = "My User Agent";
        request.Headers = headers;

        return CefReturnValue.Continue;
    }
}

public class CustomRequestHandler : RequestHandler
{
    protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
    {
        return new CustomResourceRequestHandler();
    }
}

browser.RequestHandler = new CustomRequestHandler();

使用 IRequest.Headers 属性,您必须阅读headers属性,进行更改,然后重新分配。现在可以使用 SetHeaderByName / GetHeaderByName 函数获取/设置单个标头。

Using the IRequest.Headers property you must read the headers property, make changes then reassign it. It's now possible to use the SetHeaderByName/GetHeaderByName functions to get/set a single header.

  • RequestHandler API Doc
  • ResourceRequestHandler API Doc
  • IRequest.GetHeaderByName API Doc
  • IRequest.SetHeaderByName API Doc

这篇关于铬-在初始页面加载时发送自定义标头信息c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆