首页 > 解决方案 > java.net.UnknownHostException 打开到 https 站点的连接

问题描述

自从我开始 Java 编程以来已经一个月了。与此同时,我正在研究在经过身份验证的代理单元上运行的 java fx webview。我能够在运行此代码的 http 站点上连接:

Properties systemSettings = System.getProperties();
    String User = "myusername";
    String Password = "mypassword";
    systemSettings.put("proxySet", "true");
    systemSettings.put("https.proxyHost", "192.1.1.20");
    systemSettings.put("https.proxyPort", "8888");
    systemSettings.put("http.proxyHost", "192.1.1.20");
    systemSettings.put("http.proxyPort", "8888");
    Authenticator.setDefault(new ProxyAuthenticator(User ,Password));

    HttpURLConnection conn = (HttpURLConnection) new URL("http://www.google.com/").openConnection();
    String encodedUserPwd = Base64Converter.encode("myuser:mypassword".getBytes());

    conn.setRequestProperty("Proxy-Authorization", "Basic " + encodedUserPwd);

    conn.connect();


    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

    String inputLine;
    while ((inputLine = in.readLine()) != null) {
    }

    WebView webView = new WebView();
    WebEngine engine = webView.getEngine();
    engine.load("http://www.google.com/");

    VBox root = new VBox();
    root.getChildren().addAll(webView);
    Scene scene = new Scene(root, 1200, 700);

    primaryStage.setScene(scene);
    primaryStage.show();
    in.close();

但如果我将 url 更改为 https,它会抛出 java.net.UnknownHostException。需要帮忙。

标签: javaauthenticationjavafxwebviewproxy

解决方案


推荐阅读