按照承诺,我最终会得到解决方案:
javascript调用了一个不存在的网页。请求和资源处理程序捕获此调用并返回自己的anwser。当实现自己的请求/资源处理程序时,即使您不需要此功能,在某些情况下也必须替换NotImplementedException。在这种情况下,只需访问官方文档即可了解默认行为:http://cefsharp.github.io/api/75.1.x/html/T_CefSharp_IRequestHandler.htm
XAML:
<Window x:Class="Chromium.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
...
xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<cefSharp:ChromiumWebBrowser
x:Name="ChromiumWebBrowser"
/>
</Grid>
</Window>
MainWindow.Xaml.cs
public MainWindow()
{
InitializeComponent();
ChromiumWebBrowser.BrowserSettings.WebSecurity = CefState.Disabled;
ChromiumWebBrowser.RequestHandler = new RequestHandler();
ChromiumWebBrowser.Address = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"TestHtml","index.html");
}
index.html
<!doctype html>
<html>
<head>
<link href="css/style.css" rel="stylesheet" type="text/css">
<script src="js/script.js" type="text/javascript"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<h1>Title</h1>
<p id="result"></p>
<button onclick="call()">Call</button>
</body>
JS脚本
function call() {
axios.get('http://doesnt_exist')
.then((response) => {
console.log(response);
document.getElementById("result").innerHTML = JSON.stringify(response.data);
});
}
请求处理程序
public class RequestHandler : IRequestHandler
{
public IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser,IBrowser browser,IFrame frame,IRequest request,bool isNavigation,bool isDownload,string requestInitiator,ref bool disableDefaultHandling)
{
Console.WriteLine("GetResourceRequestHandler " + request.Url);
return new ResourceRequestHandler();
}
...
}
ResourceRequestHandler
class ResourceRequestHandler : IResourceRequestHandler
{
public IResourceHandler GetResourceHandler(IWebBrowser chromiumWebBrowser,IRequest request)
{
Console.WriteLine("GetResourceHandler " + request.Url);
if (request.Url.Contains("doesnt_exist"))
return ResourceHandler.FromString("Hello");
return null;
}
...
}
本文链接:https://www.f2er.com/3015661.html