You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

连接HTTPS站点时出现java.net.UnknownHostException问题求助

Fixing JavaFX WebView HTTPS Proxy UnknownHostException

Hey there, let's break down why your HTTPS connections are failing with java.net.UnknownHostException even though HTTP works. The core issue here is that your current proxy configuration is incomplete for HTTPS requests—you've set up the host and port for HTTPS, but missed passing the authentication credentials for HTTPS proxy access, and there are a few other tweaks to ensure JavaFX WebView picks up the settings correctly.

Step 1: Complete Proxy Authentication Configuration

Your code defines a username and password, but doesn't apply them to the HTTPS proxy properties. HTTP might work if your proxy allows unauthenticated HTTP traffic, but HTTPS likely requires valid credentials to route requests. Add these missing properties:

Properties systemSettings = System.getProperties();
String user = "myusername";
String password = "mypassword";
String proxyHost = "192.1.1.20";
String proxyPort = "8888";

// Configure HTTP proxy with auth
systemSettings.put("http.proxyHost", proxyHost);
systemSettings.put("http.proxyPort", proxyPort);
systemSettings.put("http.proxyUser", user);
systemSettings.put("http.proxyPassword", password);

// Configure HTTPS proxy with auth (this was missing!)
systemSettings.put("https.proxyHost", proxyHost);
systemSettings.put("https.proxyPort", proxyPort);
systemSettings.put("https.proxyUser", user);
systemSettings.put("https.proxyPassword", password);

// Optional: Skip proxy for local hosts (adjust as needed)
systemSettings.put("http.nonProxyHosts", "localhost|127.0.0.1");
systemSettings.put("https.nonProxyHosts", "localhost|127.0.0.1");

// Note: proxySet is not required in Java 1.5+, but you can keep it if needed
// systemSettings.put("proxySet", "true");

Step 2: Ensure JavaFX WebView Respects System Proxy Settings

Occasionally, JavaFX WebView might not automatically inherit system proxy properties. If the above fix doesn't work, try explicitly configuring the ProxySelector for the WebEngine:

// After setting system properties, create a custom ProxySelector
ProxySelector proxySelector = new ProxySelector() {
    @Override
    public List<Proxy> select(URI uri) {
        if (uri.getScheme().startsWith("http") || uri.getScheme().startsWith("https")) {
            InetSocketAddress proxyAddr = new InetSocketAddress(proxyHost, Integer.parseInt(proxyPort));
            return Collections.singletonList(new Proxy(Proxy.Type.HTTP, proxyAddr));
        }
        return Collections.singletonList(Proxy.NO_PROXY);
    }

    @Override
    public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
        // Handle connection failure if needed
        ioe.printStackTrace();
    }
};

// Assign the selector to the WebEngine
webView.getEngine().setProxySelector(proxySelector);

Step 3: Verify Proxy Server Compatibility

Double-check that your proxy server actually supports HTTPS traffic on port 8888. Some proxies use different ports for HTTP and HTTPS, or require additional configuration to forward HTTPS requests. Since HTTP works, the host and port are reachable, but confirm with your proxy admin that HTTPS routing is enabled for your credentials.

Why This Works

The UnknownHostException usually happens when Java can't resolve the target HTTPS hostname. If your proxy requires authentication for HTTPS requests, failing to provide credentials means the proxy won't forward your DNS lookup or request, leading Java to think the host doesn't exist. By adding the HTTPS proxy auth properties, you let the proxy properly route your HTTPS traffic.

内容的提问来源于stack exchange,提问作者Kervin Rey Balibagoso

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:57:31