Ⅰ. はじめに
タイトルの通り「CefSharpでリダイレクトURLを取得する方法」です。
CefSharpに関する基本的な情報はこちら
http://kagasu.hatenablog.com/entry/2017/12/16/154109
Platform | 動作確認 |
WPF | OK |
WinForms | NG |
2018/02/11時点でWinForms版CefSharpはCefSharp.IRequestHandlerに対応していないので動作しません。
Ⅱ. サンプルプログラム
MainWindow.xaml
public MainWindow() { InitializeComponent(); var browser = new ChromiumWebBrowser(); browser.RequestHandler = new MyRequestHandler(); browser.Load("http://127.0.0.1/test.php"); Content = browser; }
test.php
<?php header("HTTP/1.1 302"); header("Location: http://example.com");
MyRequestHandler.cs
using CefSharp; using System; using System.Security.Cryptography.X509Certificates; class MyRequestHandler : IRequestHandler { bool IRequestHandler.OnBeforeBrowse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, bool isRedirect) { if (isRedirect) { Console.WriteLine($"リダイレクトURL: {request.Url}"); } return false; } bool IRequestHandler.OnOpenUrlFromTab(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture) { return false; } bool IRequestHandler.OnCertificateError(IWebBrowser browserControl, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback) { return false; } void IRequestHandler.OnPluginCrashed(IWebBrowser browserControl, IBrowser browser, string pluginPath) { } CefReturnValue IRequestHandler.OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback) { return CefReturnValue.Continue; } bool IRequestHandler.GetAuthCredentials(IWebBrowser browserControl, IBrowser browser, IFrame frame, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback) { callback.Dispose(); return false; } void IRequestHandler.OnRenderProcessTerminated(IWebBrowser browserControl, IBrowser browser, CefTerminationStatus status) { } bool IRequestHandler.OnQuotaRequest(IWebBrowser browserControl, IBrowser browser, string originUrl, long newSize, IRequestCallback callback) { return false; } bool IRequestHandler.OnProtocolExecution(IWebBrowser browserControl, IBrowser browser, string url) { return false; } void IRequestHandler.OnRenderViewReady(IWebBrowser browserControl, IBrowser browser) { } bool IRequestHandler.OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response) { return false; } void IRequestHandler.OnResourceLoadComplete(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength) { } public bool OnSelectClientCertificate(IWebBrowser browserControl, IBrowser browser, bool isProxy, string host, int port, X509Certificate2Collection certificates, ISelectClientCertificateCallback callback) { return false; } public void OnResourceRedirect(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response, ref string newUrl) { } public IResponseFilter GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response) { return null; } }
実行結果
参考
- WPFでCefSharp(Chromiumの.NET向け実装)を使う - 3
https://qiita.com/GHKEN/items/1de12740c13350efec67 - CefSharp の IRequestHandler は最低限どう実装すればよいか
https://peta.okechan.net/blog/archives/4672 - ChromiumWebBrowser URL changed event
https://github.com/cefsharp/CefSharp/issues/1437