【问题标题】:Display webview with basic authentication inside program在程序内显示具有基本身份验证的 webview
【发布时间】:2017-06-06 10:26:05
【问题描述】:

我必须在我的Java 软件中显示webpage。(你可以认为它是Wireless Device 配置的页面之一) 但是当我要在浏览器中转到该页面时,浏览器会显示userpassword 弹出窗口,我输入userpassword 并转到该页面。(注意用户名始终是root,但密码可以不同) 现在我要从Java软件显示页面,我可以链接页面并通过Java打开页面,但无线设备内的主机显示: 401 Unauthorized,我要显示的页面的网址是http://192.168.1.2/Wireless 我使用Fiddler 来监控它的行为,该URL 不断地重新加载。 这是 Get 方法的标题:

GET /Wireless HTTP/1.1
Host: 192.168.1.2
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://192.168.1.2/Wireless
Authorization: Basic xxxxxxxxx
Connection: keep-alive

我该怎么做?与HttpClient ? 有人知道该怎么做吗?并给我一个解决方案? 或者样品或任何东西可以帮助我? 谢谢。

编辑: 这是我显示 webview 的代码:

         myFrame.setSize(mainJTabbed.getSize());
     myFrame.setLocationRelativeTo(mainJTabbed);
    myFrame.setVisible(true);
    myFrame.add(myFXPanel);    
    Platform.runLater(() -> {
      BorderPane borderPane = new BorderPane();
      WebView webComponent = new WebView();

      webComponent.getEngine().load("myURL");

      borderPane.setCenter(webComponent);
      Scene scene = new Scene(borderPane,450,450);
      myFXPanel.setScene(scene);
});

即使是 GET 方法,用户和密码也不会通过URL 发布。

【问题讨论】:

  • 你看过这个example吗?如果这不是重复的,请编辑您的问题以包含 minimal reproducible example,以显示您修改后的方法。
  • 感谢您的回复先生,但正如您所见,它是一个 GET 方法,但它不会在 url 中显示任何内容,只是 URL。网页显示身份验证弹出窗口,然后转到 192.168.1.2/wireless

标签: java javafx webview get


【解决方案1】:

我是这样做的,我用构造函数创建了一个类,一切都在构造函数中,注意我的构造函数是我想要的,你可以改变它,但是算法是这样的:

    import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebView;
import javax.swing.JFrame;
public class WebViewSample {

    public WebViewSample(String urlString, JFrame myFrame, JFXPanel myFXPanel) {

        try {
        // Sets the authenticator that will be used by the networking code
            // when a proxy or an HTTP server asks for authentication.
            Authenticator.setDefault(new CustomAuthenticator());

            URL url = new URL("http://" +urlString + "/wireless");
                            Platform.runLater(() -> {
                            BorderPane borderPane = new BorderPane();
                            WebView webComponent = new WebView();

                            webComponent.getEngine().load(url.toString());

                            borderPane.setCenter(webComponent);
                            Scene scene = new Scene(borderPane,450,450);
                            myFXPanel.setScene(scene);
                        });
                    }
        catch (MalformedURLException e) {
            System.out.println("Malformed URL: " + e.getMessage());
        }
        catch (IOException e) {
            System.out.println("I/O Error: " + e.getMessage());
        }       
    }   
    public static class CustomAuthenticator extends Authenticator {

        // Called when password authorization is needed
        protected PasswordAuthentication getPasswordAuthentication() {

            // Get information about the request
            String prompt = getRequestingPrompt();
            String hostname = getRequestingHost();
            InetAddress ipaddr = getRequestingSite();
            int port = getRequestingPort();

            String username = "myUserName";
            String password = "myPassword";
            // Return the information (a data holder that is used by Authenticator)
            return new PasswordAuthentication(username, password.toCharArray());            
        }       
    }
}

【讨论】:

    猜你喜欢
    • 2019-09-27
    • 2015-09-05
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 2013-03-03
    • 1970-01-01
    相关资源
    最近更新 更多