CEFSharp-如何从内部版本75.1.143访问OnResourceResponse [英] CEFSharp - How can I access OnResourceResponse from build 75.1.143

查看:129
本文介绍了CEFSharp-如何从内部版本75.1.143访问OnResourceResponse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从Visual Studio决定将其更新为内部版本77.1.18以来,我一直在CEFSharp中度过一个杀人的时间,而Nuget搞砸了整个过程,只安装了三个版本,但拒绝卸载其中任何一个!为了解决这些问题,我必须手动编辑各种解决方案和项目文件,并进行安装,以便我已安装75版并且可以正常工作.

I have been having a murderous time with CEFSharp in WinForms since Visual Studio decided to update it to build 77.1.18 and Nuget messed the whole thing up leaving three versions installed but refusing to uninstall any of them! In order to resolve the issues I had to manually edit various solution and project files, and got it so that I have build 75 installed and mostly working.

然后,系统通知我DefaultRequestHandler已经过时了,我应该改用RequestHandler.很好,并且需要进行一些更改,例如将替代设置为受保护而不是公开.全部完成,除了一件事:OnResourceResponse.它坚持认为这不再存在.我已经搜索了所有可以找到的内容,并且文档中的所有引用以及Google搜索等内容都引用了比较旧的build 55.不幸的是,出于手头的特殊目的,我需要能够捕获任何非200的响应,并将错误信息传回视图,然后再将其关闭.

The system then notified me that DefaultRequestHandler was now obsolete, and that I should use RequestHandler instead. That is fine, and required a few changes such as making the overrides protected instead of public. All done, except for one thing: OnResourceResponse. It insists that this no longer exists. I have searched everything I can find, and all references in the documentation and Google searches etc. refer to build 55 which is rather old. Unfortunately, for the particular purpose at hand I need to be able to trap any response that is not a 200 and pass the error information back to the view before closing it.

根据我能够找到的文档,RequestHandler实现了IRequestHandler,该IRequestHandler应该包含此事件处理程序,但是Intellisense不会为我提供任何实现,并且如果我忽略Intellisense,显然我将无法编译...

According to the docs I have been able to find, RequestHandler implements IRequestHandler which should contain this event handler but Intellisense does not offer me any implementation and if I ignore the Intellisense then obviously I cannot compile...

这是我使用的原始代码,需要复制:

Here is the original code I was using, and need to replicate:

    public override bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
    {
        //NOTE: You cannot modify the response, only the request
        // You can now access the headers
        //var headers = response.Headers;

        // If it is not an OK response, set the error and close the dialog
        if (response.StatusCode != (int)HttpStatusCode.OK)
        {
            var error = $"{response.StatusCode}: {response.StatusText}";
            var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;
            var parent = (MyBrowserForm)chromiumWebBrowser.Parent.Parent.Parent;
            parent.Errors = error;
            browser.CloseBrowser(true);
            return true;
        }

        return false;
    }

请问有人可以告诉我如何使它像更新前一样工作吗?

Please can anyone tell me how to get this to work as it did before the update?

要进行最终测试,因为我仍然遇到其他(不同的)nuget问题,因此答案与接受的答案中所建议的有点类似.万一其他人需要它,这就是我要做的:

Subject to final testing because I am still having other (different) nuget issues, the answer was sort of as suggested in the accepted answer. In case anyone else needs it, here is what I had to do:

public class MyBrowserResourceRequestHandler : ResourceRequestHandler
{
    protected override bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
    {
        //NOTE: You cannot modify the response, only the request
        // You can now access the headers
        //var headers = response.Headers;

        // If it is not an OK response, set the error and close the dialog
        if (response.StatusCode != (int)HttpStatusCode.OK)
        {
            var error = $"{response.StatusCode}: {response.StatusText}";
            var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;
            var parent = (MyBrowserForm)chromiumWebBrowser.Parent.Parent.Parent;
            parent.Errors = error;
            browser.CloseBrowser(true);
            return true;
        }

        return false;
    }
}

然后,在原始类中,添加此重写的方法:

Then, in the original class, add this overridden method:

    protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
    {
        return new MyBrowserResourceRequestHandler();
    }

现在,它们都可以正确编译,并且初始测试看起来不错.如有必要,我将进行更新.

It now all compiles correctly, and initial tests look good. I will update if any further changes prove necessary.

推荐答案

看来,在版本75中,您应该改为使用 ResourceRequestHandler ,这样您就可以从此类继承并使用它.可以在其中找到 OnResourceResponse .

It appears that in version 75, you should look into ResourceRequestHandler instead, so you you will inherit from this class and use it. OnResourceResponse can be found inside it.

以下是 ResourceRequestHandler.cs 中的示例代码,您可以在CEFSharp中找到

The following is a sample code from ResourceRequestHandler.cs which you can find in the CEFSharp repository inside CefSharp/CefSharp/Handler/ResourceRequestHandler.cs

  bool IResourceRequestHandler.OnResourceResponse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
  {
      return OnResourceResponse(chromiumWebBrowser, browser, frame, request, response);
  }

因此,您只需要从 ResourceRequestHandler 覆盖 OnResourceResponse :

So you need just to override OnResourceResponse from the ResourceRequestHandler:

protected override bool OnResourceResponse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
   //Do whatever you like
}

希望这对您有所帮助.

这篇关于CEFSharp-如何从内部版本75.1.143访问OnResourceResponse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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