JxBrowser 6.11 and higher provides the API that allows you to handle URL requests for standard (e.g. HTTP, HTTPS, FTP, etc.) and non-standard (e.g. JAR, MYPROTOCOL, etc.) protocols. The following example demonstrates how to register protocol handler for standard HTTPS protocol and response with custom data:(JxBrowser 6.11和更高版本提供了API,该API允许您处理标准(例如HTTP,HTTPS,FTP等)和非标准(例如JAR,MYPROTOCOL等)协议的URL请求。以下示例演示了如何为标准HTTPS协议注册协议处理程序以及如何使用自定义数据进行响应:)

import com.teamdev.jxbrowser.chromium.*;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;

public class ProtocolHandlerSample {
    public static void main(String[] args) {
        Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        BrowserContext browserContext = browser.getContext();
        ProtocolService protocolService = browserContext.getProtocolService();
        protocolService.setProtocolHandler("https", new ProtocolHandler() {
            @Override
            public URLResponse onRequest(URLRequest request) {
                URLResponse response = new URLResponse();
                String html = "<html><body><p>Hello there!</p></body></html>";
                response.setData(html.getBytes());
                response.getHeaders().setHeader("Content-Type", "text/html");
                return response;
            }
        });

        browser.loadURL("https://google.com/");
    }
}

You can use the same way to handle custom non-standard protocols (e.g. "teamdev"). For example:(您可以使用相同的方式来处理自定义非标准协议(例如“ teamdev”)。例如:)

import com.teamdev.jxbrowser.chromium.*;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;

public class ProtocolHandlerSample {
    public static void main(String[] args) {
        Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        BrowserContext browserContext = browser.getContext();
        ProtocolService protocolService = browserContext.getProtocolService();
        protocolService.setProtocolHandler("teamdev", new ProtocolHandler() {
            @Override
            public URLResponse onRequest(URLRequest request) {
                URLResponse response = new URLResponse();
                String html = "<html><body><p>Hello there!</p></body></html>";
                response.setData(html.getBytes());
                response.getHeaders().setHeader("Content-Type", "text/html");
                return response;
            }
        });

        browser.loadURL("teamdev://custom-request/");
    }
}

It is also possible to register a custom protocol handler for JAR protocol. It allows you to load HTML files directly from JAR libraries included into your application class path:(也可以为JAR协议注册自定义协议处理程序。它允许您直接从应用程序类路径中包含的JAR库中加载HTML文件:)

import com.teamdev.jxbrowser.chromium.*;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;
import java.io.DataInputStream;
import java.io.InputStream;
import java.net.URL;

public class ProtocolHandlerSample {
    public static void main(String[] args) {
        final Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        BrowserContext browserContext = browser.getContext();
        ProtocolService protocolService = browserContext.getProtocolService();
        protocolService.setProtocolHandler("jar", new ProtocolHandler() {
            @Override
            public URLResponse onRequest(URLRequest request) {
                try {
                    URLResponse response = new URLResponse();
                    URL path = new URL(request.getURL());
                    InputStream inputStream = path.openStream();
                    DataInputStream stream = new DataInputStream(inputStream);
                    byte[] data = new byte[stream.available()];
                    stream.readFully(data);
                    response.setData(data);
                    String mimeType = getMimeType(path.toString());
                    response.getHeaders().setHeader("Content-Type", mimeType);
                    return response;
                } catch (Exception ignored) {}
                return null;
            }
        });

        // Assume that we need to load a resource related to this class in the JAR file
        browser.loadURL(ProtocolHandlerSample.class.getResource("index.html").toString());
    }

    private static String getMimeType(String path) {
        if (path.endsWith(".html")) {
            return "text/html";
        }
        if (path.endsWith(".css")) {
            return "text/css";
        }
        if (path.endsWith(".js")) {
            return "text/javascript";
        }
        return "text/html";
    }
}